From 9b9279b87c79166e5746a27772f8b7fab310df08 Mon Sep 17 00:00:00 2001 From: Shivam Sahu <54952935+supershivam13@users.noreply.github.com> Date: Sun, 4 Oct 2020 01:40:50 +0530 Subject: [PATCH] Add files via upload --- BinaryNumberUsingQueue.cpp | 34 +++++++++++ Counting Divisors(using Sieve).cpp | 95 ++++++++++++++++++++++++++++++ Factorial of BIG INTEGER.cpp | 40 +++++++++++++ Fibbonaci Numbers using Queue.cpp | 14 +++++ GCD - Euclid's Algorithm.cpp | 16 +++++ Insertion Sort.cpp | 26 ++++++++ Long Prime Check(using Sieve).cpp | 95 ++++++++++++++++++++++++++++++ 7 files changed, 320 insertions(+) create mode 100644 BinaryNumberUsingQueue.cpp create mode 100644 Counting Divisors(using Sieve).cpp create mode 100644 Factorial of BIG INTEGER.cpp create mode 100644 Fibbonaci Numbers using Queue.cpp create mode 100644 GCD - Euclid's Algorithm.cpp create mode 100644 Insertion Sort.cpp create mode 100644 Long Prime Check(using Sieve).cpp diff --git a/BinaryNumberUsingQueue.cpp b/BinaryNumberUsingQueue.cpp new file mode 100644 index 0000000..dfa13c7 --- /dev/null +++ b/BinaryNumberUsingQueue.cpp @@ -0,0 +1,34 @@ +// CPP function to generate all binary number +// from 0 to given number n +#include +#include +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; +} diff --git a/Counting Divisors(using Sieve).cpp b/Counting Divisors(using Sieve).cpp new file mode 100644 index 0000000..7cb3cd4 --- /dev/null +++ b/Counting Divisors(using Sieve).cpp @@ -0,0 +1,95 @@ +#include +using namespace std; + +#define ll long long + + + +//Seive Approach - Generating a array containing prime numbers +vector 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 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 &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 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; + + +} \ No newline at end of file diff --git a/Factorial of BIG INTEGER.cpp b/Factorial of BIG INTEGER.cpp new file mode 100644 index 0000000..7d94175 --- /dev/null +++ b/Factorial of BIG INTEGER.cpp @@ -0,0 +1,40 @@ +#include +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=0) + { + cout< +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; +} \ No newline at end of file diff --git a/Insertion Sort.cpp b/Insertion Sort.cpp new file mode 100644 index 0000000..f8abd48 --- /dev/null +++ b/Insertion Sort.cpp @@ -0,0 +1,26 @@ +#include +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; + } +} diff --git a/Long Prime Check(using Sieve).cpp b/Long Prime Check(using Sieve).cpp new file mode 100644 index 0000000..7cb3cd4 --- /dev/null +++ b/Long Prime Check(using Sieve).cpp @@ -0,0 +1,95 @@ +#include +using namespace std; + +#define ll long long + + + +//Seive Approach - Generating a array containing prime numbers +vector 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 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 &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 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; + + +} \ No newline at end of file