Paste: Project Euler problem 4

Author: pozorvlak
Mode: factor
Date: Wed, 14 Oct 2009 20:51:34
Plain Text |
! Copyright (C) 2009 Your name.
! See http://factorcode.org/license.txt for BSD license.
! Find the largest palindrome made from the product of two 3-digit numbers.
USING: math math.parser math.functions math.ranges kernel sequences lists.lazy ;
IN: project-euler.problem4

: palindrome? ( str -- ? ) dup reverse = ;
: numeric-palindrome? ( n -- ? ) number>string palindrome? ;
: 10to ( n -- 10^n ) 10 swap ^ ;
: digit-nums ( n -- seq ) [ 1 - ] keep [ 10to ] bi@ 1 - [a,b] reverse ;
: a*b ( a,b -- ab) [ first ] keep second * ;
! : digit-nums-list ( n -- list ) digit-nums sequence>list ;
! : pair-digit-nums ( n -- seq seq ) digit-nums-list dup ;
! : digit-num-pairs ( n -- seq ) pair-digit-nums lcartesian-product ;
! : products ( seq -- seq ) [ a*b ] lazy-map ;
! : first-palindrome ( seq -- n ) [ palindrome? ] lfilter car ;
! : solution ( n -- n ) digits-nums-pairs products first-palindrome ;

Annotation: Version that actually compiles - thanks to erg

Author: pozorvlak
Mode: factor
Date: Wed, 14 Oct 2009 22:50:27
Plain Text |
! Copyright (C) 2009 Your name.
! See http://factorcode.org/license.txt for BSD license.
! Find the largest palindrome made from the product of two 3-digit numbers.
USING: math math.parser math.functions math.ranges kernel sequences lists.lazy 
lists ;
IN: project-euler.problem4

: palindrome? ( str -- ? ) dup reverse = ;
: numeric-palindrome? ( n -- ? ) number>string palindrome? ;
: digit-nums ( n -- seq ) [ 1 - ] keep [ 10^ ] bi@ 1 - [a,b] reverse ;
: a*b ( a,b -- ab ) [ first ] keep second * ;
: digit-nums-list ( n -- list ) digit-nums sequence>list ;
: pair-digit-nums ( n -- seq seq ) digit-nums-list dup ; 
: digit-num-pairs ( n -- seq ) pair-digit-nums lcartesian-product ; 
: products ( seq -- seq ) [ a*b ] lazy-map ; 
: first-palindrome ( seq -- n ) [ palindrome? ] lfilter car ; 
: solution ( n -- n ) digit-num-pairs products first-palindrome ;

New Annotation

Summary:
Author:
Mode:
Body: