forked from rachitiitr/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
345d8cd
commit 9b9279b
Showing
7 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// CPP function to generate all binary number | ||
// from 0 to given number n | ||
#include<iostream> | ||
#include<stdlib.h> | ||
using namespace std; | ||
|
||
// Maximum length of generated binary number | ||
const int MAX = 100; | ||
|
||
// CPP function to generate all binary number | ||
// for given number n | ||
char binaryGenerator(int n) | ||
{ | ||
char a[MAX]; | ||
|
||
for (int i = 0; i <= n; i++) { | ||
|
||
// use define function itoa() to convert | ||
// in given base | ||
// a is char[] array where value store | ||
// 2 is base, which convert. | ||
itoa(i, a, 2); | ||
|
||
cout << a << endl; | ||
} | ||
} | ||
|
||
// Driven program | ||
int main() | ||
{ | ||
int n = 10; | ||
binaryGenerator(n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define ll long long | ||
|
||
|
||
|
||
//Seive Approach - Generating a array containing prime numbers | ||
vector<int> PrimeSieve(int *p, int n) { | ||
|
||
//first all odd numbers as prime as they can be potential prime | ||
for (int i = 3; i <= 1000000; i += 2) { | ||
p[i] = 1; | ||
} | ||
//Sieve | ||
for (ll i = 3; i <= 1000000; i += 2) { | ||
//if current number is not marked (it is prime) | ||
if (p[i] == 1) { | ||
//mark all the multiples of i as not prime | ||
for (ll j = i * i; j <= 1000000; j = j + i) { | ||
p[j] = 0; | ||
} | ||
} | ||
} | ||
//Special cases | ||
p[2] = 1; | ||
p[1] = p[0] = 0; | ||
|
||
vector<int> primes; | ||
primes.push_back(2); | ||
|
||
for (int i = 3; i <= n; i += 2) { | ||
if (p[i] == 1) | ||
primes.push_back(i); | ||
|
||
} | ||
|
||
return primes; | ||
|
||
|
||
} | ||
|
||
int no_of_divisors(int m, vector<int> &primes) { | ||
|
||
int i = 0; | ||
int p = primes[0]; | ||
int ans = 1; | ||
|
||
while (p * p <= m) { | ||
|
||
if (m % p == 0) { | ||
int count = 0; | ||
|
||
while (m % p == 0) { | ||
count++; | ||
m = m / p; | ||
} | ||
ans = ans * (count + 1); | ||
} | ||
// go to next position | ||
i++; | ||
p = primes[i]; | ||
} | ||
|
||
//if m is not reduced to 1 , it means m also a prime number | ||
if (m != 1) { | ||
ans = ans * 2; | ||
} | ||
return ans; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
int main() { | ||
|
||
|
||
int p[1000000] = {0}; | ||
vector<int> primes = PrimeSieve(p, 100000); | ||
|
||
int t; cin >> t; | ||
while (t--) { | ||
int no; cin >> no; | ||
int divs = no_of_divisors(no, primes); | ||
cout << divs << endl; | ||
|
||
|
||
|
||
} | ||
|
||
return 0; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
|
||
int q=2; | ||
int arr[100000] = {0}; | ||
arr[0] = 1; | ||
int len = 1; | ||
int x = 0; | ||
int num = 0; | ||
while(q<=n) | ||
{ | ||
x=0; | ||
num =0; | ||
while(x<len) | ||
{ | ||
arr[x] = arr[x] *q; | ||
arr[x] = arr[x]+num; | ||
num = arr[x]/10; | ||
arr[x] = arr[x]%10; | ||
x++; | ||
} | ||
while(num!=0) | ||
{ | ||
arr[len] = num%10; | ||
num = num/10; | ||
len++; | ||
} | ||
q++; | ||
} | ||
len--; | ||
while(len>=0) | ||
{ | ||
cout<<arr[len]; | ||
len = len-1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
void fun(int n) | ||
{ | ||
IntQueue q = new IntQueue(); | ||
q.enqueue(0); | ||
q.enqueue(1); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int a = q.dequeue(); | ||
int b = q.dequeue(); | ||
q.enqueue(b); | ||
q.enqueue(a + b); | ||
print(a); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int gcd(int a, int b) { | ||
|
||
return b == 0 ? a : gcd(b, a % b); | ||
} | ||
|
||
|
||
|
||
int main() { | ||
int n1, n2; | ||
cin >> n1 >> n2; | ||
|
||
cout << gcd(n1, n2) << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
void insertion_sort(int [], int ); | ||
|
||
int main() { | ||
int n; cin >> n; | ||
int arr[n]; | ||
for (int i = 0; i < n; i++) { | ||
cin >> arr[i]; | ||
} | ||
|
||
insertion_sort(arr, n); | ||
for (int f = 0; f < n; f++) cout << arr[f] << " "; | ||
|
||
} | ||
|
||
|
||
void insertion_sort(int arr[], int n) { | ||
int i, j, temp; | ||
for (i = 1; i < n; i++) { | ||
temp = arr[i]; | ||
for (j = i - 1; j >= 0 && temp < arr[j]; j--) | ||
{ arr[j + 1] = arr[j]; } | ||
arr[j + 1] = temp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define ll long long | ||
|
||
|
||
|
||
//Seive Approach - Generating a array containing prime numbers | ||
vector<int> PrimeSieve(int *p, int n) { | ||
|
||
//first all odd numbers as prime as they can be potential prime | ||
for (int i = 3; i <= 1000000; i += 2) { | ||
p[i] = 1; | ||
} | ||
//Sieve | ||
for (ll i = 3; i <= 1000000; i += 2) { | ||
//if current number is not marked (it is prime) | ||
if (p[i] == 1) { | ||
//mark all the multiples of i as not prime | ||
for (ll j = i * i; j <= 1000000; j = j + i) { | ||
p[j] = 0; | ||
} | ||
} | ||
} | ||
//Special cases | ||
p[2] = 1; | ||
p[1] = p[0] = 0; | ||
|
||
vector<int> primes; | ||
primes.push_back(2); | ||
|
||
for (int i = 3; i <= n; i += 2) { | ||
if (p[i] == 1) | ||
primes.push_back(i); | ||
|
||
} | ||
|
||
return primes; | ||
|
||
|
||
} | ||
|
||
int no_of_divisors(int m, vector<int> &primes) { | ||
|
||
int i = 0; | ||
int p = primes[0]; | ||
int ans = 1; | ||
|
||
while (p * p <= m) { | ||
|
||
if (m % p == 0) { | ||
int count = 0; | ||
|
||
while (m % p == 0) { | ||
count++; | ||
m = m / p; | ||
} | ||
ans = ans * (count + 1); | ||
} | ||
// go to next position | ||
i++; | ||
p = primes[i]; | ||
} | ||
|
||
//if m is not reduced to 1 , it means m also a prime number | ||
if (m != 1) { | ||
ans = ans * 2; | ||
} | ||
return ans; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
int main() { | ||
|
||
|
||
int p[1000000] = {0}; | ||
vector<int> primes = PrimeSieve(p, 100000); | ||
|
||
int t; cin >> t; | ||
while (t--) { | ||
int no; cin >> no; | ||
int divs = no_of_divisors(no, primes); | ||
cout << divs << endl; | ||
|
||
|
||
|
||
} | ||
|
||
return 0; | ||
|
||
|
||
} |