TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
C++ Program to calculate the number of positive divisors. More...
#include <cassert>
#include <iostream>
Go to the source code of this file.
Functions | |
int | number_of_positive_divisors (int n) |
void | tests () |
int | main () |
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, for N = -36 the number of positive divisors remain same.
Definition in file number_of_positive_divisors.cpp.
int main | ( | void | ) |
Main function
Definition at line 81 of file number_of_positive_divisors.cpp.
int number_of_positive_divisors | ( | int | n | ) |
Function to compute the number of positive divisors.
n | number to compute divisors for |
Definition at line 33 of file number_of_positive_divisors.cpp.
void tests | ( | ) |
Test implementations
Definition at line 70 of file number_of_positive_divisors.cpp.