]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/099/099.factor
Switch to https urls
[factor.git] / extra / project-euler / 099 / 099.factor
1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: io.encodings.ascii io.files kernel math math.functions
4 math.parser math.vectors project-euler.common sequences
5 sequences.extras splitting ;
6 IN: project-euler.099
7
8 ! https://projecteuler.net/index.php?section=problems&id=99
9
10 ! DESCRIPTION
11 ! -----------
12
13 ! Comparing two numbers written in index form like 2^11 and 3^7 is not difficult,
14 ! as any calculator would confirm that 2^11 = 2048 < 3^7 = 2187.
15
16 ! However, confirming that 632382^518061 519432^525806 would be much more
17 ! difficult, as both numbers contain over three million digits.
18
19 ! Using base_exp.txt (right click and 'Save Link/Target As...'), a 22K text
20 ! file containing one thousand lines with a base/exponent pair on each line,
21 ! determine which line number has the greatest numerical value.
22
23 ! NOTE: The first two lines in the file represent the numbers in the example
24 ! given above.
25
26
27 ! SOLUTION
28 ! --------
29
30 ! Use logarithms to make the calculations necessary more manageable.
31
32 <PRIVATE
33
34 : source-099 ( -- seq )
35     "resource:extra/project-euler/099/base_exp.txt"
36     ascii file-lines [ "," split [ string>number ] map ] map ;
37
38 : simplify ( seq -- seq )
39     ! exponent * log(base)
40     flip first2 swap [ log ] map v* ;
41
42 : solve ( seq -- index )
43     simplify arg-max 1 + ;
44
45 PRIVATE>
46
47 : euler099 ( -- answer )
48     source-099 solve ;
49
50 ! [ euler099 ] 100 ave-time
51 ! 16 ms ave run timen - 1.67 SD (100 trials)
52
53 SOLUTION: euler099