From 5ab8a5088b729a9953b8dff1b2a985dc8fb2098b Mon Sep 17 00:00:00 2001 From: mzuenni Date: Mon, 27 Jun 2022 17:19:28 +0200 Subject: updated tcr --- math/piLegendre.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 math/piLegendre.cpp (limited to 'math/piLegendre.cpp') diff --git a/math/piLegendre.cpp b/math/piLegendre.cpp new file mode 100644 index 0000000..f45ee85 --- /dev/null +++ b/math/piLegendre.cpp @@ -0,0 +1,23 @@ +constexpr ll cache = 500; // requires O(cache^3) +vector> memo(cache * cache, vector(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(primes.begin(), primes.end(), n)); + } else { + ll k = pi(sqrtl(n) + 1); + return n - phi(n, k) + k; +}} -- cgit v1.2.3