1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See http://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 ;
8 ! http://projecteuler.net/index.php?section=problems&id=99
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.
16 ! However, confirming that 632382^518061 519432^525806 would be much more
17 ! difficult, as both numbers contain over three million digits.
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.
23 ! NOTE: The first two lines in the file represent the numbers in the example
30 ! Use logarithms to make the calculations necessary more manageable.
34 : source-099 ( -- seq )
35 "resource:extra/project-euler/099/base_exp.txt"
36 ascii file-lines [ "," split [ string>number ] map ] map ;
38 : simplify ( seq -- seq )
39 ! exponent * log(base)
40 flip first2 swap [ log ] map v* ;
42 : solve ( seq -- index )
43 simplify arg-max 1 + ;
47 : euler099 ( -- answer )
50 ! [ euler099 ] 100 ave-time
51 ! 16 ms ave run timen - 1.67 SD (100 trials)