factor/extra/project-euler/036/036.factor

41 lines
988 B
Factor
Raw Normal View History

2008-01-29 13:31:06 -05:00
! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
2008-02-08 22:29:12 -05:00
USING: combinators.lib kernel math.parser math.ranges project-euler.common
sequences combinators.short-circuit ;
2008-01-29 13:31:06 -05:00
IN: project-euler.036
! http://projecteuler.net/index.php?section=problems&id=36
! DESCRIPTION
! -----------
! The decimal number, 585 = 1001001001 (binary), is palindromic in both bases.
! Find the sum of all numbers, less than one million, which are palindromic in
! base 10 and base 2.
! (Please note that the palindromic number, in either base, may not include
! leading zeros.)
! SOLUTION
! --------
! Only check odd numbers since the binary number must begin and end with 1
<PRIVATE
: both-bases? ( n -- ? )
2008-02-08 22:29:12 -05:00
{ [ dup palindrome? ]
2008-06-10 21:42:55 -04:00
[ dup >bin dup reverse = ] } 0&& nip ;
2008-01-29 13:31:06 -05:00
PRIVATE>
: euler036 ( -- answer )
1 1000000 2 <range> [ both-bases? ] filter sum ;
2008-01-29 13:31:06 -05:00
! [ euler036 ] 100 ave-time
! 3891 ms run / 173 ms GC ave time - 100 trials
MAIN: euler036