Skip to content

Commit

Permalink
added data for hacktober fest 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritik-Banger-Biz4group committed Oct 1, 2022
1 parent 71b4352 commit 3899345
Show file tree
Hide file tree
Showing 1,625 changed files with 99,586 additions and 2 deletions.
62 changes: 62 additions & 0 deletions 01. Mathematics/10_exactly 3 divisors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// { Driver Code Starts
//Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
//User function Template for C++

class Solution
{
public:
int exactly3Divisors(int n)
{
cout << "Numbers with 3 divisors : "<< endl;
for(int i = 2; i * i <= n; i++)
{
// Checks for prime
if (isPrime(i)) //if prime checks if its square is less than or equal to n
if (i * i <= n)
{
// Print numbers
cout << i * i << " ";
}
}
}
return 0;
}
//method for checking whether number is prime or not
bool isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
};

// { Driver Code Starts.

int main()
{
int T;

//taking testcases
cin >> T;
while (T--)
{
int N;

//taking N
cin >> N;
Solution ob;
//calling function exactly3Divisors()
cout << ob.exactly3Divisors(N) << endl;
}
return 0;
} // } Driver Code Ends
44 changes: 44 additions & 0 deletions 01. Mathematics/11_GPterm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// { Driver Code Starts
//Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
//User function Template for C++

class Solution
{
public:
//Complete this function
double termOfGP(int A, int B, int N)
{
//first finding ratio
double ratio = double(B)/double(A);
//Nth term according to the formula
double ans = A*(pow(ratio,N-1));
return ans;
}
};

// { Driver Code Starts.

int main()
{
int T; //testcases total
cin >> T; //input the testcases

for (int i = 0; i < T; i++) //white testcases are not exhausted
{
int A, B;
cin >> A >> B; //input first and second term of gp
int N;
cin >> N; //input n
Solution ob;
cout << floor(ob.termOfGP(A, B, N)) << endl;
}

return 0;
}

// } Driver Code Ends
61 changes: 61 additions & 0 deletions 01. Mathematics/12_Missing-number-in-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.
Example 1:
Input:
N = 5
A[] = {1,2,3,5}
Output: 4
Example 2:
Input:
N = 10
A[] = {1,2,3,4,5,6,7,8,10}
Output: 9
Your Task :
You don't need to read input or print anything.
Complete the function MissingNumber() that takes array and N as input parameters and returns the value of the missing number.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
*/

#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
int MissingNumber(vector<int> &array, int n)
{
// Your code goes here
int i=0,sum=0,missing_num;
int sum_of_n=n*(n+1)/2;//sum of first n natural numbers
for(i=0;i<n-1;i++)
sum+=array[i];//sum of elements present in array.
missing_num=sum_of_n-sum;//missing element
return missing_num;
}
};

int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;

vector<int> array(n - 1);
for (int i = 0; i < n - 1; ++i)
cin >> array[i];
Solution obj;
cout << obj.MissingNumber(array, n) << "\n";
}
return 0;
}
41 changes: 41 additions & 0 deletions 01. Mathematics/13_ModularMultiplicativeInverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// { Driver Code Starts
//Initial Template for C++

#include <iostream>
using namespace std;

// } Driver Code Ends
//User function Template for C++

class Solution
{
public:
//Complete this function
int modInverse(int a, int m)
{
for (int x = 1; x < m; x++)
if (((a%m) * (x%m)) % m == 1)
return x;
}
};

// { Driver Code Starts.

int main()
{
int T;

//taking testcases
cin >> T;
while (T--)
{
int a, m;

//taking input a and m
cin >> a >> m;
Solution ob;
//calling function modInverse()
cout << ob.modInverse(a, m) << endl;
}
return 0;
} // } Driver Code Ends
45 changes: 45 additions & 0 deletions 01. Mathematics/14_primalityTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// { Driver Code Starts
//Initial Template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
//User function Template for C++

class Solution
{
public:
bool isPrime(int N)
{
for(int i=2;i<=sqrt(N);i++)
{
if(N%i==0)
return false;
}
return true;
//Your code here
}
};

// { Driver Code Starts.

int main()
{
int T; //testcases
cin >> T; //input testcases
while (T--) //while testcase have not been exhausted
{
int N;
cin >> N; //input n
Solution ob;
if (ob.isPrime(N))
cout << "Yes";
else
cout << "No";

cout << endl;
}
return 0;
}
// } Driver Code Ends
65 changes: 65 additions & 0 deletions 01. Mathematics/15_QuadraticEquationRoots.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// { Driver Code Starts
// Initial template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
// User function Template for C++

class Solution
{
public:
vector<int> quadraticRoots(int a, int b, int c)
{
vector<int> ans;
if(a==0)
{
ans.push_back(-1);
return ans;
}
int d = b*b - 4*a*c;
double sqrt_val = sqrt(abs(d));
if(d>0)
{
double root1 = (double)(-b + sqrt_val)/(2*a);
double root2 = (double)(-b - sqrt_val)/(2*a);
ans.push_back(root1);
ans.push_back(root2);
}
else if(d==0)
{
double root = -(double)b/(2*a);
ans.push_back(root);
ans.push_back(root);
}
else
{
ans.push_back(-1);
}
return ans;
// code here
}
};

// { Driver Code Starts.

int main()
{
int T;
cin >> T;
while (T--)
{
int a, b, c;
cin >> a >> b >> c;
Solution ob;
vector<int> roots = ob.quadraticRoots(a, b, c);
if (roots.size() == 1 && roots[0] == -1)
cout << "Imaginary";
else
for (int i = 0; i < roots.size(); i++)
cout << roots[i] << " ";
cout << endl;
}
return 0;
} // } Driver Code Ends
36 changes: 36 additions & 0 deletions 01. Mathematics/16_Smallestdivisiblenumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// { Driver Code Starts
//Initial template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
//User function template for C++

class Solution
{
public:
long long getSmallestDivNum(long long n)
{
long long ans = 1;
for(long long i=1;i<=n;i++)
ans = (ans*i)/(__gcd(ans,i));
return ans;
// code here
}
};

// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
Solution ob;
cout << ob.getSmallestDivNum(n) << endl;
}
return 0;
} //
Loading

0 comments on commit 3899345

Please sign in to comment.