diff --git a/d0/da2/number__of__positive__divisors_8cpp.html b/d0/da2/number__of__positive__divisors_8cpp.html index e879d733f..1bc5cb194 100644 --- a/d0/da2/number__of__positive__divisors_8cpp.html +++ b/d0/da2/number__of__positive__divisors_8cpp.html @@ -97,10 +97,10 @@ $(document).ready(function(){initNavTree('d0/da2/number__of__positive__divisors_
-

C++ Program to calculate number of divisors. +

C++ Program to calculate the number of positive divisors. More...

-
#include <iostream>
-#include <vector>
+
#include <cassert>
+#include <iostream>
Include dependency graph for number_of_positive_divisors.cpp:
@@ -112,26 +112,22 @@ Include dependency graph for number_of_positive_divisors.cpp:
Functions int number_of_positive_divisors (int n)   +void tests () +  int main ()  

Detailed Description

-

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.

-

Example:-
-N = 36
-36 = (3^2 * 2^2)
-number_of_positive_divisors(36) = (2+1) * (2+1) = 9.
+

C++ Program to calculate the number of positive divisors.

+

This algorithm uses the prime factorization approach. Any positive integer can be written as a product of its prime factors.
+Let \(N = p_1^{e_1} \times p_2^{e_2} \times\cdots\times p_k^{e_k}\) where \(p_1,\, p_2,\, \dots,\, p_k\) are distinct prime factors of \(N\) and \(e_1,\, e_2,\, \dots,\, e_k\) are respective positive integer exponents.
+Each positive divisor of \(N\) is in the form \(p_1^{g_1}\times p_2^{g_2}\times\cdots\times p_k^{g_k}\) where \(0\le g_i\le e_i\) are integers for all \(1\le i\le k\).
+Finally, there are \((e_1+1) \times (e_2+1)\times\cdots\times (e_k+1)\) positive divisors of \(N\) since we can choose every \(g_i\) independently.

+

Example:
+ \(N = 36 = (3^2 \cdot 2^2)\)
+ \(\mbox{number_of_positive_divisors}(36) = (2+1) \cdot (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.

+

Similarly, for N = -36 the number of positive divisors remain same.

Function Documentation

◆ main()

@@ -148,23 +144,22 @@ list of positive divisors of -36 = 1, 2, 3, 4, 6, 9, 12, 18, 36.

Main function

-
62  {
-
63  int n;
-
64  std::cin >> n;
-
65  if (n < 0) {
-
66  n = -n;
-
67  }
-
68  if (n == 0) {
-
69  std::cout << "All non-zero numbers are divisors of 0 !" << std::endl;
-
70  } else {
-
71  std::cout << "Number of positive divisors is : ";
- -
73  }
-
74 }
+
81  {
+
82  tests();
+
83  int n;
+
84  std::cin >> n;
+
85  if (n == 0) {
+
86  std::cout << "All non-zero numbers are divisors of 0 !" << std::endl;
+
87  } else {
+
88  std::cout << "Number of positive divisors is : ";
+ +
90  }
+
91  return 0;
+
92 }
Here is the call graph for this function:
-
+
@@ -185,35 +180,75 @@ Here is the call graph for this function:
-

Algorithm

-
34  {
-
35  std::vector<int> prime_exponent_count;
-
36  for (int i = 2; i * i <= n; i++) {
-
37  int prime_count = 0;
-
38  while (n % i == 0) {
-
39  prime_count += 1;
-
40  n /= i;
-
41  }
-
42  if (prime_count != 0) {
-
43  prime_exponent_count.push_back(prime_count);
-
44  }
-
45  }
-
46  if (n > 1) {
-
47  prime_exponent_count.push_back(1);
-
48  }
-
49 
-
50  int divisors_count = 1;
-
51 
-
52  for (int i = 0; i < prime_exponent_count.size(); i++) {
-
53  divisors_count = divisors_count * (prime_exponent_count[i] + 1);
-
54  }
-
55 
-
56  return divisors_count;
-
57 }
+

Function to compute the number of positive divisors.

Parameters
+ + +
nnumber to compute divisors for
+
+
+
Returns
number of positive divisors of n (or 1 if n = 0)
+
33  {
+
34  if (n < 0) {
+
35  n = -n; // take the absolute value of n
+
36  }
+
37 
+
38  int number_of_divisors = 1;
+
39 
+
40  for (int i = 2; i * i <= n; i++) {
+
41  // This part is doing the prime factorization.
+
42  // Note that we cannot find a composite divisor of n unless we would
+
43  // already previously find the corresponding prime divisor and dvided
+
44  // n by that prime. Therefore, all the divisors found here will
+
45  // actually be primes.
+
46  // The loop terminates early when it is left with a number n which
+
47  // does not have a divisor smaller or equal to sqrt(n) - that means
+
48  // the remaining number is a prime itself.
+
49  int prime_exponent = 0;
+
50  while (n % i == 0) {
+
51  // Repeatedly divide n by the prime divisor n to compute
+
52  // the exponent (e_i in the algorithm description).
+
53  prime_exponent++;
+
54  n /= i;
+
55  }
+
56  number_of_divisors *= prime_exponent + 1;
+
57  }
+
58  if (n > 1) {
+
59  // In case the remaining number n is a prime number itself
+
60  // (essentially p_k^1) the final answer is also multiplied by (e_k+1).
+
61  number_of_divisors *= 2;
+
62  }
+
63 
+
64  return number_of_divisors;
+
65 }
+
+
+
+ +

◆ tests()

+ +
+
+ + + + + + + +
void tests ()
+
+

Test implementations

+
70  {
+
71  assert(number_of_positive_divisors(36) == 9);
+
72  assert(number_of_positive_divisors(-36) == 9);
+
73  assert(number_of_positive_divisors(1) == 1);
+
74  assert(number_of_positive_divisors(2011) == 2); // 2011 is a prime
+
75  assert(number_of_positive_divisors(756) == 24); // 756 = 2^2 * 3^3 * 7
+
76 }
Here is the call graph for this function:
-
+
@@ -221,12 +256,10 @@ Here is the call graph for this function:
- -
T size(T... args)
-
T push_back(T... args)
+
void tests()
Definition: number_of_positive_divisors.cpp:70
T endl(T... args)
-
int number_of_positive_divisors(int n)
Definition: number_of_positive_divisors.cpp:34
+
int number_of_positive_divisors(int n)
Definition: number_of_positive_divisors.cpp:33