I guess the punch line of this one is Sieving for primes.
#include <cmath> #include <cstdio> #include <climits> #include <cctype> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; vector<int> primes; int digiSum(int n) { int ret = 0; while (n) { ret += n % 10; n /= 10; } return ret; } int facSum(int n) { int ret = 0; int pinx = 0; while (n > 1 && pinx < primes.size()) { int p = primes[pinx]; while (n % p == 0) { n /= p; ret += digiSum(p); } pinx++; } if (n > 1) { ret += digiSum(n); } return ret; } void go(int n) { int dSum = digiSum(n); int fSum = facSum(n); cout << (dSum == fSum ? 1 : 0) << endl; } void sieving(int n) { int bound = ceil(sqrt(n)); vector<bool> mark(bound + 1, true); int removed = 1; int picked = 2; while (removed) { removed = 0; for (int i = 2 * picked; i <= bound; i += picked) { mark[i] = false; removed++; } picked++; while (!mark[picked]) picked++; } for (int i = 2; i < mark.size(); i ++) if (mark[i]) primes.push_back(i); } int main() { sieving(INT_MAX); int n; cin >> n; go(n); return 0; }