Update solution to Project Euler problem 92
parent
a34c5aabed
commit
9c2218a1c4
|
@ -1,7 +1,6 @@
|
||||||
! Copyright (c) 2008 Aaron Schaefer.
|
! Copyright (c) 2008 Aaron Schaefer.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: combinators.lib kernel math math.ranges namespaces project-euler.common
|
USING: kernel math math.ranges project-euler.common sequences ;
|
||||||
sequences ;
|
|
||||||
IN: project-euler.092
|
IN: project-euler.092
|
||||||
|
|
||||||
! http://projecteuler.net/index.php?section=problems&id=92
|
! http://projecteuler.net/index.php?section=problems&id=92
|
||||||
|
@ -30,7 +29,7 @@ IN: project-euler.092
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: next-link ( n -- m )
|
: next-link ( n -- m )
|
||||||
number>digits [ sq ] sigma ;
|
0 swap [ dup zero? not ] [ 10 /mod sq -rot [ + ] dip ] [ ] while drop ;
|
||||||
|
|
||||||
: chain-ending ( n -- m )
|
: chain-ending ( n -- m )
|
||||||
dup 1 = over 89 = or [ next-link chain-ending ] unless ;
|
dup 1 = over 89 = or [ next-link chain-ending ] unless ;
|
||||||
|
@ -41,15 +40,15 @@ IN: project-euler.092
|
||||||
: fast-chain-ending ( seq n -- m )
|
: fast-chain-ending ( seq n -- m )
|
||||||
dup 567 > [ next-link ] when 1- swap nth ;
|
dup 567 > [ next-link ] when 1- swap nth ;
|
||||||
|
|
||||||
|
: count ( seq quot -- n )
|
||||||
|
0 -rot [ rot >r call [ r> 1+ ] [ r> ] if ] curry each ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: euler092 ( -- answer )
|
: euler092 ( -- answer )
|
||||||
lower-endings 9999999 [1,b] [ fast-chain-ending 89 = ] with count ;
|
lower-endings 9999999 [1,b] [ fast-chain-ending 89 = ] with count ;
|
||||||
|
|
||||||
! [ euler092 ] time
|
! [ euler092 ] 10 ave-time
|
||||||
! 68766 ms run / 372 ms GC time
|
! 11169 ms run / 0 ms GC ave time - 10 trials
|
||||||
|
|
||||||
! TODO: solution is still too slow, maybe try using a 10000000-byte array that
|
|
||||||
! keeps track of each number in the chain and their endings
|
|
||||||
|
|
||||||
MAIN: euler092
|
MAIN: euler092
|
||||||
|
|
Loading…
Reference in New Issue