diff options
| author | Paul Jungeblut <paul.jungeblut@gmail.com> | 2017-02-24 11:01:57 +0100 |
|---|---|---|
| committer | Paul Jungeblut <paul.jungeblut@gmail.com> | 2017-02-24 11:01:57 +0100 |
| commit | 376a77de38a4b552200734bb3c721f0fd01dfc72 (patch) | |
| tree | 8d9de389f56d52313616be47fc72c2309d592b47 /math/phi.cpp | |
| parent | cbc62f94c241b5567b1e1dfafad837710ed4961d (diff) | |
Lot's of small changes to math chapter.
Diffstat (limited to 'math/phi.cpp')
| -rw-r--r-- | math/phi.cpp | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/math/phi.cpp b/math/phi.cpp index c8f8900..492a9d2 100644 --- a/math/phi.cpp +++ b/math/phi.cpp @@ -1,8 +1,11 @@ -ll phi(ll n) { +ll phi(ll n) { // Laufzeit: O(sqrt(n)) + // Optimierung: Falls n prim, n - 1 zurückgeben (Miller-Rabin/Sieb). ll result = n; - for(int i = 2; i * i <= n; ++i) if(n % i == 0) { - while(n % i == 0)n /= i; - result -= result / i; + for(int i = 2; i * i <= n; ++i) { + if(n % i == 0) { // Optimierung: Nur über Primzahlen iterieren. + while(n % i == 0)n /= i; + result -= result / i; + } } if(n > 1) result -= result / n; return result; |
