From f432c0f5840d1151e4b4491abc55303e8dc8d4d9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 16:22:10 -0400 Subject: [PATCH] documetnation for positive divisors --- math/number_of_positive_divisors.cpp | 46 ++++++++++++++++------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/math/number_of_positive_divisors.cpp b/math/number_of_positive_divisors.cpp index 48ab63c36..f157f7b41 100644 --- a/math/number_of_positive_divisors.cpp +++ b/math/number_of_positive_divisors.cpp @@ -1,34 +1,39 @@ -/// C++ Program to calculate number of divisors. - -#include -#include - /** + * @file + * @brief C++ Program to calculate number of divisors + * * 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. + *
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) - * 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. + *
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. + *
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. * **/ +#include +#include + +/** + * Algorithm + */ int number_of_positive_divisors(int n) { std::vector prime_exponent_count; - for (int i=2; i*i <= n; i++) { + for (int i = 2; i * i <= n; i++) { int prime_count = 0; while (n % i == 0) { prime_count += 1; @@ -44,13 +49,16 @@ int number_of_positive_divisors(int n) { int divisors_count = 1; - for (int i=0; i < prime_exponent_count.size(); i++) { - divisors_count = divisors_count * (prime_exponent_count[i]+1); + for (int i = 0; i < prime_exponent_count.size(); i++) { + divisors_count = divisors_count * (prime_exponent_count[i] + 1); } return divisors_count; } +/** + * Main function + */ int main() { int n; std::cin >> n;