Paste: euler #2
Author: | indraastra |
Mode: | factor |
Date: | Thu, 20 Nov 2008 10:26:25 |
Plain Text |
USING: kernel math sequences ;
IN: euler.002
<PRIVATE
: (next-fibs) ( x y -- y x+y )
swap over + ;
: (max-exceeded?) ( max total fib- fib+ -- max total fib- fib+ bool )
roll 2dup > >r -roll r> ;
: (+total) ( total fib- fib+ -- newtotal fib- fib+ )
rot over + -rot ;
: (sum-even-fibs-below) ( max total fib- fib+ -- sum )
(max-exceeded?)
[ 2drop swap drop ]
[
dup even? [ (+total) ] [ ] if
(next-fibs) (sum-even-fibs-below)
]
if ;
PRIVATE>
: sum-even-fibs-below ( max -- sum )
0 0 1 (sum-even-fibs-below) ;
: euler002 ( -- answer )
4000000 sum-even-fibs-below ;
MAIN: euler002
Author: | indraastra |
Mode: | factor |
Date: | Tue, 2 Dec 2008 04:03:14 |
Plain Text |
<PRIVATE
: next-fibs ( x y -- y x+y )
swap over + ;
: max-exceeded? ( fib+ max -- bool )
> ;
: ?retotal ( total fib- fib+ -- retotal fib- fib+ )
dup even? [ [ swap drop + ] 2keep ] [ ] if ;
: (sum-even-fibs-below) ( partial fib- fib+ max -- total )
2dup max-exceeded?
[ 3drop ]
[ [ ?retotal next-fibs ] dip (sum-even-fibs-below) ]
if ;
PRIVATE>
: sum-even-fibs-below ( max -- sum )
[ 0 0 1 ] dip (sum-even-fibs-below) ;
: euler002 ( -- answer )
4000000 sum-even-fibs-below ;
Author: | Vishal Talwar |
Mode: | factor |
Date: | Tue, 2 Dec 2008 04:10:09 |
Plain Text |
USING: kernel math sequences ;
IN: euler.002
<PRIVATE
: next-fibs ( x y -- y x+y )
tuck + ;
: ?retotal ( total fib- fib+ -- retotal fib- fib+ )
dup even? [ [ nip + ] 2keep ] when ;
: (sum-even-fibs-below) ( partial fib- fib+ max -- total )
2dup >
[ 3drop ]
[ [ ?retotal next-fibs ] dip (sum-even-fibs-below) ]
if ;
PRIVATE>
: sum-even-fibs-below ( max -- sum )
[ 0 0 1 ] dip (sum-even-fibs-below) ;
: euler002 ( -- answer )
4000000 sum-even-fibs-below ;
MAIN: euler002
New Annotation