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