]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/056/056.factor
3a727563990e5c9fa769a3ec52c2b10f2c136099
[factor.git] / extra / project-euler / 056 / 056.factor
1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math.functions math.ranges project-euler.common
4 sequences math.order ;
5 IN: project-euler.056
6
7 ! http://projecteuler.net/index.php?section=problems&id=56
8
9 ! DESCRIPTION
10 ! -----------
11
12 ! A googol (10^100) is a massive number: one followed by one-hundred zeros;
13 ! 100^100 is almost unimaginably large: one followed by two-hundred zeros.
14 ! Despite their size, the sum of the digits in each number is only 1.
15
16 ! Considering natural numbers of the form, a^b, where a, b < 100, what is the
17 ! maximum digital sum?
18
19
20 ! SOLUTION
21 ! --------
22
23 ! Through analysis, you only need to check when a and b > 90
24
25 : euler056 ( -- answer )
26     90 100 [a..b) dup cartesian-product concat
27     [ first2 ^ number>digits sum ] [ max ] map-reduce ;
28
29 ! [ euler056 ] 100 ave-time
30 ! 22 ms ave run time - 2.13 SD (100 trials)
31
32 SOLUTION: euler056