You are viewing a single comment's thread. Return to all comments →
And the same code in C++.
#include <cstdio> #include <vector> #include <set> using namespace std; long distinct_powers(long n) { long c = 0; vector<bool> tested(n+1, false); for (long a = 2; a <= n; a++) if (!tested[a]) { set<long> s; for (long _a = a*a, pw = 2; _a <= n; _a *= a, pw++) { tested[_a] = true; for (long b = 2, bxpw = b*pw; b <= n; b++, bxpw += pw) if (bxpw > n) s.insert(bxpw); } c += s.size() + n-1; } return c; } int main() { long n; scanf("%ld", &n); printf("%ld\n", distinct_powers(n)); return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #29: Distinct powers
You are viewing a single comment's thread. Return to all comments →
And the same code in C++.