Skip to content

Commit 4b740d4

Browse files
fix: fit euler's totient to the contribution guidelines (#2447)
* fix: fit euler's totient to the contribution guidelines. * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <[email protected]> * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <[email protected]> * fix: add more tests notes: i should have added euler's number first * fix: revert description * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <[email protected]> * Update math/eulers_totient_function.cpp Co-authored-by: David Leal <[email protected]> * chore: apply suggestions from code review * chore: apply suggestions from code review --------- Co-authored-by: David Leal <[email protected]>
1 parent a022701 commit 4b740d4

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

math/eulers_totient_function.cpp

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
22
* @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
75
* Euler Totient Function is also known as phi function.
86
* \f[\phi(n) =
97
* \phi\left({p_1}^{a_1}\right)\cdot\phi\left({p_2}^{a_2}\right)\ldots\f] where
@@ -23,36 +21,58 @@
2321
* * \f$\phi(1) = 1\f$
2422
* * \f$\phi(17501) = 15120\f$
2523
* * \f$\phi(1420) = 560\f$
24+
* @author [Mann Mehta](https://github.com/mann2108)
2625
*/
27-
#include <cstdlib>
28-
#include <iostream>
2926

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
3138
*/
3239
uint64_t phiFunction(uint64_t n) {
3340
uint64_t result = n;
3441
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;
4146
}
42-
if (n > 1)
43-
result -= result / n;
47+
if (n > 1) result -= result / n;
48+
4449
return result;
4550
}
51+
} // namespace math
4652

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+
*/
4875
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();
5777
return 0;
5878
}

0 commit comments

Comments
 (0)