TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
eulers_totient_function.cpp
Go to the documentation of this file.
1
28
#include <cassert>
29
#include <cstdint>
30
#include <iostream>
31
36
namespace
math
{
41
uint64_t
phiFunction
(uint64_t n) {
42
uint64_t result = n;
43
for
(uint64_t i = 2; i * i <= n; i++) {
44
if
(n % i != 0)
45
continue
;
46
while
(n % i == 0) n /= i;
47
48
result -= result / i;
49
}
50
if
(n > 1)
51
result -= result / n;
52
53
return
result;
54
}
55
}
// namespace math
56
61
static
void
test
() {
62
assert(
math::phiFunction
(1) == 1);
63
assert(
math::phiFunction
(2) == 1);
64
assert(
math::phiFunction
(10) == 4);
65
assert(
math::phiFunction
(123456) == 41088);
66
assert(
math::phiFunction
(808017424794) == 263582333856);
67
assert(
math::phiFunction
(3141592) == 1570792);
68
assert(
math::phiFunction
(27182818) == 12545904);
69
70
std::cout <<
"All tests have successfully passed!\n"
;
71
}
72
79
int
main
(
int
argc,
char
*argv[]) {
80
test
();
81
return
0;
82
}
test
static void test()
Self-test implementations.
Definition
eulers_totient_function.cpp:61
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
math
for assert
math::phiFunction
uint64_t phiFunction(uint64_t n)
Function to calculate Euler's Totient.
Definition
eulers_totient_function.cpp:41
math
eulers_totient_function.cpp
Generated by
1.12.0