diff options
| author | Gloria Mundi <gloria@gloria-mundi.eu> | 2024-11-16 17:48:10 +0100 |
|---|---|---|
| committer | Gloria Mundi <gloria@gloria-mundi.eu> | 2024-11-16 18:01:53 +0100 |
| commit | e55df069a8f83b2c0c2b56c035f49e89516cdaaa (patch) | |
| tree | dd6767e3fc6ac8532661dc75886a3056804d1d46 /content/math/piLegendre.cpp | |
| parent | 72bd993483453ed8ebc462f1a33385cd355d486f (diff) | |
minor fixes, let code breathe where possible
Diffstat (limited to 'content/math/piLegendre.cpp')
| -rw-r--r-- | content/math/piLegendre.cpp | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/content/math/piLegendre.cpp b/content/math/piLegendre.cpp index 21b974b..46c8584 100644 --- a/content/math/piLegendre.cpp +++ b/content/math/piLegendre.cpp @@ -1,23 +1,23 @@ -constexpr ll cache = 500; // requires O(cache^3)
-vector<vector<ll>> memo(cache * cache, vector<ll>(cache));
-
-ll pi(ll n);
-
-ll phi(ll n, ll k) {
- if (n <= 1 || k < 0) return 0;
- if (n <= primes[k]) return n - 1;
- if (n < N && primes[k] * primes[k] > n) return n - pi(n) + k;
- bool ok = n < cache * cache;
- if (ok && memo[n][k] > 0) return memo[n][k];
- ll res = n/primes[k] - phi(n/primes[k], k - 1) + phi(n, k - 1);
- if (ok) memo[n][k] = res;
- return res;
-}
-
-ll pi(ll n) {
- if (n < N) { // implement this as O(1) lookup for speedup!
- return distance(primes.begin(), upper_bound(all(primes), n));
- } else {
- ll k = pi(sqrtl(n) + 1);
- return n - phi(n, k) + k;
-}}
+constexpr ll cache = 500; // requires O(cache^3) +vector<vector<ll>> memo(cache * cache, vector<ll>(cache)); + +ll pi(ll n); + +ll phi(ll n, ll k) { + if (n <= 1 || k < 0) return 0; + if (n <= primes[k]) return n - 1; + if (n < N && primes[k] * primes[k] > n) return n - pi(n) + k; + bool ok = n < cache * cache; + if (ok && memo[n][k] > 0) return memo[n][k]; + ll res = n/primes[k] - phi(n/primes[k], k - 1) + phi(n, k - 1); + if (ok) memo[n][k] = res; + return res; +} + +ll pi(ll n) { + if (n < N) { // implement this as O(1) lookup for speedup! + return distance(primes.begin(), upper_bound(all(primes), n)); + } else { + ll k = pi(sqrtl(n) + 1); + return n - phi(n, k) + k; +}} |
