Paste: my shameful euler.002

Author: ageldama
Mode: factor
Date: Sat, 14 Feb 2009 06:16:37
Plain Text |
! Copyright (C) 2009 Yun, Jonghyouk.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel locals math sequences sequences.last ;
IN: math.fibonacci

: fib-next ( seq -- n ) [ last-0 ] [ last-1 ] bi + ;

: fib-next-push ( seq -- newseq ) dup fib-next suffix ;

: fib-initial-seq ( -- seq ) { 1 1 } ;

: fib-produce-seq ( seq max -- newseq )
    [let | max_ [ ]
           seq_ [ ] |
        seq_ fib-next max_ <
        [ seq_ fib-next-push max_ fib-produce-seq ]
        [ seq_ ]
        if ] ;

Annotation: another shameful

Author: ageldama
Mode: factor
Date: Sat, 14 Feb 2009 06:17:21
Plain Text |
! Copyright (C) 2009 Yun, Jonghyouk.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences ;
IN: sequences.last

: last-n ( seq n -- obj ) swap dup length rot - swap nth ;

: last-0 ( seq -- obj ) 1 last-n ;

: last-1 ( seq -- obj ) 2 last-n ;

Annotation: euler.002

Author: ageldama
Mode: factor
Date: Sat, 14 Feb 2009 06:17:35
Plain Text |
! Copyright (C) 2009 Yun, Jonghyouk.
! See http://factorcode.org/license.txt for BSD license.
USING: io kernel locals math math.fibonacci prettyprint sequences
sequences.last ;
IN: euler.002


: fib-limit-n ( -- n ) 4000000 ;

: even-fibs ( -- seq )
    fib-initial-seq fib-limit-n fib-produce-seq [ even? ] filter ;

: solve ( -- )
    even-fibs sum pprint "" print ;


MAIN: solve

New Annotation

Summary:
Author:
Mode:
Body: