fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int powi(int x, int e)
  7. {
  8. int y = 1;
  9. while(e) {
  10. if (e&1) y *= x;
  11. x*=x;
  12. e >>= 1;
  13. }
  14. return y;
  15. }
  16.  
  17. int main(int argc, char * argv[])
  18. {
  19. for(int n = 1; n <= 10000; ++n)
  20. {
  21. int d = 0, m = n;
  22. while(m) { d += m%10; m /= 10; }
  23. int e = 1;
  24. if (d > 1) e = round(log(n)/log(d));
  25. if (powi(d,e) == n)
  26. cout << n << ": " << d << "^" << e << " = " << n << endl;
  27. }
  28. }
  29.  
  30.  
  31.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
1: 1^1 = 1
2: 2^1 = 2
3: 3^1 = 3
4: 4^1 = 4
5: 5^1 = 5
6: 6^1 = 6
7: 7^1 = 7
8: 8^1 = 8
9: 9^1 = 9
81: 9^2 = 81
512: 8^3 = 512
2401: 7^4 = 2401
4913: 17^3 = 4913
5832: 18^3 = 5832