diff options
| author | Paul Jungeblut <paul.jungeblut@gmail.com> | 2017-10-22 19:53:16 +0200 |
|---|---|---|
| committer | Paul Jungeblut <paul.jungeblut@gmail.com> | 2017-10-22 19:53:16 +0200 |
| commit | 31588dc1212d353adb58fbffd207b60b4868b23f (patch) | |
| tree | 3f76147a0e8107ab2f3718850bdaea152dbc000f /math/phi.cpp | |
| parent | 4b48621b48d3248b5e2718a10e35e7276c504a72 (diff) | |
Adding sieve implementation for Euler's Totient function.
Diffstat (limited to 'math/phi.cpp')
| -rw-r--r-- | math/phi.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/math/phi.cpp b/math/phi.cpp index 492a9d2..f568bba 100644 --- a/math/phi.cpp +++ b/math/phi.cpp @@ -10,3 +10,11 @@ ll phi(ll n) { // Laufzeit: O(sqrt(n)) if(n > 1) result -= result / n; return result; } + +// Sieb, falls alle Werte benötigt werden. Laufzeit: O(N*log(log(N))) +for (int i = 1; i <= N; i++) phi[i] = i; +for (int i = 2; i <= N; i++) if (phi[i] == i) { + for (int j = i; j <= N; j += i) { + phi[j] /= i; + phi[j] *= i - 1; +}} |
