PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/extra/project-euler/004/004.factor

http://github.com/abeaumont/factor
Unknown | 37 lines | 24 code | 13 blank | 0 comment | 0 complexity | 676c28c9ec7f48079700a75d0836f821 MD5 | raw file
 1! Copyright (c) 2007 Aaron Schaefer, Daniel Ehrenberg.
 2! See http://factorcode.org/license.txt for BSD license.
 3USING: hashtables kernel math math.functions math.ranges project-euler.common
 4    sequences sorting sets ;
 5IN: project-euler.004
 6
 7! http://projecteuler.net/index.php?section=problems&id=4
 8
 9! DESCRIPTION
10! -----------
11
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.
14
15! Find the largest palindrome made from the product of two 3-digit numbers.
16
17
18! SOLUTION
19! --------
20
21<PRIVATE
22
23: source-004 ( -- seq )
24    100 999 [a,b] [ 10 divisor? not ] filter ;
25
26: max-palindrome ( seq -- palindrome )
27    natural-sort [ palindrome? ] find-last nip ;
28
29PRIVATE>
30
31: euler004 ( -- answer )
32    source-004 dup [ * ] cartesian-map combine max-palindrome ;
33
34! [ euler004 ] 100 ave-time
35! 1164 ms ave run time - 39.35 SD (100 trials)
36
37SOLUTION: euler004