Paste: happy?
Author: | rand |
Mode: | factor |
Date: | Fri, 6 Aug 2010 09:47:08 |
Plain Text |
USING: assocs kernel math math.parser namespaces sequences ;
IN: happy-number
SYMBOL: repeating-numbers
: reset-hash ( -- )
H{ } clone repeating-numbers set ;
: add-repeat-number ( n -- )
t swap repeating-numbers get set-at ;
: repeater? ( n -- ? )
repeating-numbers get key? ;
: next-happy ( x -- y )
number>string string>digits [ sq ] map sum ;
: do-happy ( number -- ? )
dup 1 = [ drop t ] [
dup repeater?
[ drop f ] [
[ add-repeat-number ]
[ next-happy do-happy ] bi
] if
] if ; inline recursive
: happy? ( number -- ? )
reset-hash do-happy ;
Author: | jon |
Mode: | factor |
Date: | Sat, 7 Aug 2010 11:05:04 |
Plain Text |
USING: combinators kernel math math.parser sequences sets ;
IN: bugtest
: squares ( n -- i ) number>string [ CHAR: 0 - sq ] [ + ] map-reduce ;
: (happy?) ( n set -- ? )
{
{ [ over 1 = ] [ 2drop t ] }
{ [ 2dup in? ] [ 2drop f ] }
[ [ adjoin ] [ [ squares ] [ (happy?) ] bi* ] 2bi ]
} cond ;
: happy? ( n -- ? ) HS{ } clone (happy?) ;
New Annotation