1 ! Copyright (c) 2007 Aaron Schaefer, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: hashtables kernel math math.parser math.ranges project-euler.common
7 ! http://projecteuler.net/index.php?section=problems&id=4
12 ! A palindromic number reads the same both ways. The largest palindrome made
13 ! from the product of two 2-digit numbers is 9009 = 91 * 99.
15 ! Find the largest palindrome made from the product of two 3-digit numbers.
21 : palindrome? ( n -- ? )
22 number>string dup reverse = ;
26 : source-004 ( -- seq )
27 100 999 [a,b] [ 10 mod zero? not ] subset ;
29 : max-palindrome ( seq -- palindrome )
30 natural-sort [ palindrome? ] find-last nip ;
34 : euler004 ( -- answer )
35 source-004 dup cartesian-product [ product ] map prune max-palindrome ;
37 ! [ euler004 ] 100 ave-time
38 ! 1608 ms run / 102 ms GC ave time - 100 trials