|
1 | 1 | /**
|
2 | 2 | * @file
|
3 |
| - * @brief C++ Program to find |
4 |
| - * [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) |
5 |
| - * function |
6 |
| - * |
| 3 | + * @brief Implementation of [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) |
| 4 | + * @description |
7 | 5 | * Euler Totient Function is also known as phi function.
|
8 | 6 | * \f[\phi(n) =
|
9 | 7 | * \phi\left({p_1}^{a_1}\right)\cdot\phi\left({p_2}^{a_2}\right)\ldots\f] where
|
|
23 | 21 | * * \f$\phi(1) = 1\f$
|
24 | 22 | * * \f$\phi(17501) = 15120\f$
|
25 | 23 | * * \f$\phi(1420) = 560\f$
|
| 24 | + * @author [Mann Mehta](https://github.com/mann2108) |
26 | 25 | */
|
27 |
| -#include <cstdlib> |
28 |
| -#include <iostream> |
29 | 26 |
|
30 |
| -/** Function to caculate Euler's totient phi |
| 27 | +#include <iostream> /// for IO operations |
| 28 | +#include <cassert> /// for assert |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief Mathematical algorithms |
| 32 | + * @namespace |
| 33 | + */ |
| 34 | +namespace math { |
| 35 | +/** |
| 36 | + * @brief Function to calculate Euler's Totient |
| 37 | + * @param n the number to find the Euler's Totient of |
31 | 38 | */
|
32 | 39 | uint64_t phiFunction(uint64_t n) {
|
33 | 40 | uint64_t result = n;
|
34 | 41 | for (uint64_t i = 2; i * i <= n; i++) {
|
35 |
| - if (n % i == 0) { |
36 |
| - while (n % i == 0) { |
37 |
| - n /= i; |
38 |
| - } |
39 |
| - result -= result / i; |
40 |
| - } |
| 42 | + if (n % i != 0) continue; |
| 43 | + while (n % i == 0) n /= i; |
| 44 | + |
| 45 | + result -= result / i; |
41 | 46 | }
|
42 |
| - if (n > 1) |
43 |
| - result -= result / n; |
| 47 | + if (n > 1) result -= result / n; |
| 48 | + |
44 | 49 | return result;
|
45 | 50 | }
|
| 51 | +} // namespace math |
46 | 52 |
|
47 |
| -/// Main function |
| 53 | +/** |
| 54 | + * @brief Self-test implementations |
| 55 | + * @returns void |
| 56 | + */ |
| 57 | +static void test() { |
| 58 | + assert(math::phiFunction(1) == 1); |
| 59 | + assert(math::phiFunction(2) == 1); |
| 60 | + assert(math::phiFunction(10) == 4); |
| 61 | + assert(math::phiFunction(123456) == 41088); |
| 62 | + assert(math::phiFunction(808017424794) == 263582333856); |
| 63 | + assert(math::phiFunction(3141592) == 1570792); |
| 64 | + assert(math::phiFunction(27182818) == 12545904); |
| 65 | + |
| 66 | + std::cout << "All tests have successfully passed!\n"; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief Main function |
| 71 | + * @param argc commandline argument count (ignored) |
| 72 | + * @param argv commandline array of arguments (ignored) |
| 73 | + * @returns 0 on exit |
| 74 | + */ |
48 | 75 | int main(int argc, char *argv[]) {
|
49 |
| - uint64_t n; |
50 |
| - if (argc < 2) { |
51 |
| - std::cout << "Enter the number: "; |
52 |
| - } else { |
53 |
| - n = strtoull(argv[1], nullptr, 10); |
54 |
| - } |
55 |
| - std::cin >> n; |
56 |
| - std::cout << phiFunction(n); |
| 76 | + test(); |
57 | 77 | return 0;
|
58 | 78 | }
|
0 commit comments