Legendre prime counting function: Difference between revisions

Removed memoization from C++ solution
(→‎Non-Memoized Version: Nim, adjustments to comments and clarifications...)
(Removed memoization from C++ solution)
Line 33:
<syntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
#include <unordered_map>
#include <vector>
 
Line 62 ⟶ 61:
int phi(int x, int a);
std::vector<int> primes;
std::unordered_map<int, std::unordered_map<int, int>> phi_cache;
};
 
Line 78 ⟶ 76:
if (a == 0)
return x;
auto&if map(a == phi_cache[x];1)
auto i = map.find return x - (ax >> 1);
ifint (ipa != map.end())primes[a - 1];
if (x <= return i->second;pa)
return result1;
int result =return phi(x, a - 1) - phi(x / primes[a - 1], a - 1);
map[a] = result;
return result;
}
 
1,777

edits