Powerful digit counts | Project Euler | Problem #63
URL to the problem page: https://projecteuler.net/problem=63
The 5-digit number, 16807=7⁵, is also a fifth power. Similarly, the 9-digit number, 134217728=8⁹, is a ninth power.
How many n-digit positive integers exist which are also an nth power?
The 5-digit number, 16807=7⁵, is also a fifth power. Similarly, the 9-digit number, 134217728=8⁹, is a ninth power.
How many n-digit positive integers exist which are also an nth power?
#include <iostream>
using namespace std;
unsigned long long power(unsigned long long a, unsigned long long b) {
unsigned long long result = 1, i;
for (i = 0; i < b; i++) {
result = result * a;
}
return result;
}
unsigned long long finddigits(unsigned long long a) {
unsigned long long counter = 1;
while (a >= 10) {
a /= 10;
counter++;
}
return counter;
}
int main()
{
unsigned long long i, j, counter = 0;
for (i = 0; i <= 20; i++) {
for (j = 0; j <= 9; j++) {
if (i == finddigits(power(j, i))) {
counter++;
}
}
}
cout << "Number of n-digit positive integers which are also an nth power is = " << counter << endl;
return 0;
}
Comments
Post a Comment