1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.parser sequences strings ;
6 ! http://projecteuler.net/index.php?section=problems&id=40
11 ! An irrational decimal fraction is created by concatenating the positive
14 ! 0.123456789101112131415161718192021...
16 ! It can be seen that the 12th digit of the fractional part is 1.
18 ! If dn represents the nth digit of the fractional part, find the value of the
19 ! following expression.
21 ! d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
29 : (concat-upto) ( n limit str -- str )
31 pick number>string over push-all rot 1+ -rot (concat-upto)
36 : concat-upto ( n -- str )
37 SBUF" " clone 1 -rot (concat-upto) ;
39 : nth-integer ( n str -- m )
40 [ 1- ] dip nth 1string string>number ;
44 : euler040 ( -- answer )
45 1000000 concat-upto { 1 10 100 1000 10000 100000 1000000 }
46 [ swap nth-integer ] with map product ;
48 ! [ euler040 ] 100 ave-time
49 ! 444 ms ave run time - 23.64 SD (100 trials)