1 ! Copyright (c) 2009 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.functions ranges project-euler.common sequences ;
6 ! http://projecteuler.net/index.php?section=problems&id=63
11 ! The 5-digit number, 16807 = 7^5, is also a fifth power. Similarly, the
12 ! 9-digit number, 134217728 = 8^9, is a ninth power.
14 ! How many n-digit positive integers exist which are also an nth power?
20 ! Only have to check from 1 to 9 because 10^n already has too many digits.
21 ! In general, x^n has n digits when:
23 ! 10^(n-1) <= x^n < 10^n
25 ! ...take the left side of that equation, solve for n to see where they meet:
27 ! n = log(10) / [ log(10) - log(x) ]
29 ! Round down since we already know that particular value of n is no good.
31 : euler063 ( -- answer )
32 9 [1..b] [ log [ 10 log dup ] dip - /i ] map-sum ;
34 ! [ euler063 ] 100 ave-time
35 ! 0 ms ave run time - 0.0 SD (100 trials)