]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/063/063.factor
project-euler: Rewrap, update links, add copyrights, tests
[factor.git] / extra / project-euler / 063 / 063.factor
1 ! Copyright (c) 2009 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.functions ranges project-euler.common
4 sequences ;
5 IN: project-euler.063
6
7 ! https://projecteuler.net/problem=63
8
9 ! DESCRIPTION
10 ! -----------
11
12 ! The 5-digit number, 16807 = 7^5, is also a fifth power.
13 ! Similarly, the 9-digit number, 134217728 = 8^9, is a ninth
14 ! power.
15
16 ! How many n-digit positive integers exist which are also an nth
17 ! power?
18
19
20 ! SOLUTION
21 ! --------
22
23 ! Only have to check from 1 to 9 because 10^n already has too
24 ! many digits. In general, x^n has n digits when:
25
26 !     10^(n-1) <= x^n < 10^n
27
28 ! ...take the left side of that equation, solve for n to see
29 ! where they meet:
30
31 !     n = log(10) / [ log(10) - log(x) ]
32
33 ! Round down since we already know that particular value of n is
34 ! no good.
35
36 : euler063 ( -- answer )
37     9 [1..b] [ log [ 10 log dup ] dip - /i ] map-sum ;
38
39 ! [ euler063 ] 100 ave-time
40 ! 0 ms ave run time - 0.0 SD (100 trials)
41
42 SOLUTION: euler063