From 877125eb8acff2dc638f3e95451e28f6d93f81bb Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Wed, 15 Apr 2020 03:00:23 +0530 Subject: [PATCH 1/7] Added number-of-divisors.cpp in math directory --- math/number-of-divisors.cpp | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 math/number-of-divisors.cpp diff --git a/math/number-of-divisors.cpp b/math/number-of-divisors.cpp new file mode 100644 index 000000000..fd2d40707 --- /dev/null +++ b/math/number-of-divisors.cpp @@ -0,0 +1,47 @@ +/// C++ Program to calculate number of divisors. + +#include +#include + +/** + * This algorithm use the prime factorization approach. + * Any number can be written in multiplication of its prime factors. + * Let N = P1^E1 * P2^E2 ... Pk^Ek + * Therefore. number-of-divisors(N) = (E1+1) * (E2+1) ... (Ek+1). + * Where P1, P2 ... Pk are prime factors and E1, E2 ... Ek are exponents respectively. + * + * Example:- N=36 + * 36 = (3^2 * 2^2) + * numbe-of-divisors(36) = (2+1) * (2+1) = 9. + * list of divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36. +**/ + +int number_of_divisors(int n) { + std::vector prime_exponent_count; + for (int i=2; i*i <= n; i++) { + int prime_count = 0; + while (n % i == 0) { + prime_count += 1; + n /= i; + } + if (prime_count != 0) { + prime_exponent_count.push_back(prime_count); + } + } + int divisors_count = 1; + // If n is prime at that time vector prime_exponent_count will remain empty. + for (int i=0; i> n; + std::cout << number_of_divisors(n) << std::endl; +} From 5ee32c63de337d260456fb625e35b8ffa13fadad Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Wed, 15 Apr 2020 03:08:49 +0530 Subject: [PATCH 2/7] Resolve typo errors --- math/number-of-divisors.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/math/number-of-divisors.cpp b/math/number-of-divisors.cpp index fd2d40707..1b87b3e5b 100644 --- a/math/number-of-divisors.cpp +++ b/math/number-of-divisors.cpp @@ -12,7 +12,7 @@ * * Example:- N=36 * 36 = (3^2 * 2^2) - * numbe-of-divisors(36) = (2+1) * (2+1) = 9. + * number-of-divisors(36) = (2+1) * (2+1) = 9. * list of divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36. **/ @@ -30,10 +30,10 @@ int number_of_divisors(int n) { } int divisors_count = 1; // If n is prime at that time vector prime_exponent_count will remain empty. - for (int i=0; i Date: Wed, 15 Apr 2020 03:13:10 +0530 Subject: [PATCH 3/7] Renamed file name to number_of_divisors.cpp from number-of-divisors.cpp --- math/{number-of-divisors.cpp => number_of_divisors.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename math/{number-of-divisors.cpp => number_of_divisors.cpp} (100%) diff --git a/math/number-of-divisors.cpp b/math/number_of_divisors.cpp similarity index 100% rename from math/number-of-divisors.cpp rename to math/number_of_divisors.cpp From 4da35f1edf90e531a44aab300f2d59af331c1546 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Wed, 15 Apr 2020 09:10:00 +0530 Subject: [PATCH 4/7] Modified number_of_divisors.cpp --- math/number_of_divisors.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/math/number_of_divisors.cpp b/math/number_of_divisors.cpp index 1b87b3e5b..5bf1e717a 100644 --- a/math/number_of_divisors.cpp +++ b/math/number_of_divisors.cpp @@ -12,11 +12,11 @@ * * Example:- N=36 * 36 = (3^2 * 2^2) - * number-of-divisors(36) = (2+1) * (2+1) = 9. - * list of divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36. + * number_of_positive_divisors(36) = (2+1) * (2+1) = 9. + * list of positive divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36. **/ -int number_of_divisors(int n) { +int number_of_positive_divisors(int n) { std::vector prime_exponent_count; for (int i=2; i*i <= n; i++) { int prime_count = 0; @@ -28,20 +28,28 @@ int number_of_divisors(int n) { prime_exponent_count.push_back(prime_count); } } + if(n > 1) { + prime_exponent_count.push_back(1); + } + int divisors_count = 1; - // If n is prime at that time vector prime_exponent_count will remain empty. + for (int i=0; i < prime_exponent_count.size(); i++) { divisors_count = divisors_count * (prime_exponent_count[i]+1); } - if (divisors_count == 1 && n != 1) { - // Prime number has exactly 2 divisors 1 and itself. - divisors_count = 2; - } + return divisors_count; } int main() { int n; std::cin >> n; - std::cout << number_of_divisors(n) << std::endl; + if(n < 0) { + n = -n; + } + if(n == 0) { + std::cout << "All non-zero numbers are divisors of 0 !" << std::endl; + } else { + std::cout << "Number of positive divisors is : " << number_of_positive_divisors(n) << std::endl; + } } From 30ee76234725af798b9a18cc316bde3e4f77db76 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Wed, 15 Apr 2020 09:11:53 +0530 Subject: [PATCH 5/7] Renamed to number_of_positive_divisors.cpp from number_of_divisors.cpp --- math/{number_of_divisors.cpp => number_of_positive_divisors.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename math/{number_of_divisors.cpp => number_of_positive_divisors.cpp} (100%) diff --git a/math/number_of_divisors.cpp b/math/number_of_positive_divisors.cpp similarity index 100% rename from math/number_of_divisors.cpp rename to math/number_of_positive_divisors.cpp From 583264da0f721cbeb6d8a8a1ea18e74e955708b9 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Wed, 15 Apr 2020 09:14:50 +0530 Subject: [PATCH 6/7] Resolve errors --- math/number_of_positive_divisors.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/math/number_of_positive_divisors.cpp b/math/number_of_positive_divisors.cpp index 5bf1e717a..461c0ca82 100644 --- a/math/number_of_positive_divisors.cpp +++ b/math/number_of_positive_divisors.cpp @@ -28,7 +28,7 @@ int number_of_positive_divisors(int n) { prime_exponent_count.push_back(prime_count); } } - if(n > 1) { + if (n > 1) { prime_exponent_count.push_back(1); } @@ -44,12 +44,13 @@ int number_of_positive_divisors(int n) { int main() { int n; std::cin >> n; - if(n < 0) { + if (n < 0) { n = -n; } - if(n == 0) { + if (n == 0) { std::cout << "All non-zero numbers are divisors of 0 !" << std::endl; } else { - std::cout << "Number of positive divisors is : " << number_of_positive_divisors(n) << std::endl; + std::cout << "Number of positive divisors is : "; + std::cout << number_of_positive_divisors(n) << std::endl; } } From 2d54bfa1e3c1c175a99cc1129e191ae8bd6c24d9 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Wed, 15 Apr 2020 09:23:00 +0530 Subject: [PATCH 7/7] Modified description --- math/number_of_positive_divisors.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/math/number_of_positive_divisors.cpp b/math/number_of_positive_divisors.cpp index 461c0ca82..48ab63c36 100644 --- a/math/number_of_positive_divisors.cpp +++ b/math/number_of_positive_divisors.cpp @@ -10,10 +10,20 @@ * Therefore. number-of-divisors(N) = (E1+1) * (E2+1) ... (Ek+1). * Where P1, P2 ... Pk are prime factors and E1, E2 ... Ek are exponents respectively. * - * Example:- N=36 + * Example:- + * N = 36 * 36 = (3^2 * 2^2) * number_of_positive_divisors(36) = (2+1) * (2+1) = 9. * list of positive divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36. + * + * Similarly if N is -36 at that time number of positive divisors remain same. + * + * Example:- + * N = -36 + * -36 = -1 * (3^2 * 2^2) + * number_of_positive_divisors(-36) = (2+1) * (2+1) = 9. + * list of positive divisors of -36 = 1, 2, 3, 4, 6, 9, 12, 18, 36. + * **/ int number_of_positive_divisors(int n) {