Quantcast
Viewing all articles
Browse latest Browse all 18

project euler-problem 63

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.

How many n-digit positive integers exist which are also an nth power?

这道题必须要先计算n为多大时, 计算就要停止, 因为你不可能无穷地对自然数进行brute-force.

要求xn为n个数长, 如果xn长度小于n,那么xn+1的长度必定小于n+1,因为x<10. 所以只要找出最小的n值, 使得xn长度小于n, 问题就变成了求解最小的n, 使得满足xn < 10n-1。解不等式, 发现n > 1/(1-log10(x))时不等式满足, 也就是说, 只有n ≤ int 1/(1-log10(x))时, xn长度为n.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include
#include<iostream>
#include<cmath>
 
using namespace std;
 
int main() {
  int res = 0;
  for (int i=1; i <= 9; i++) {
    res += 1/(1-log10(i));
  }
  cout << "Answer: " << res << endl;
}

Related Posts


Viewing all articles
Browse latest Browse all 18

Trending Articles