35 lines
894 B
Text
35 lines
894 B
Text
[ 1 & ] is odd ( n --> b )
|
|
|
|
[ []
|
|
[ over join swap
|
|
dup 1 > while
|
|
dup odd iff
|
|
[ 3 * 1 + ]
|
|
else
|
|
[ 2 / ]
|
|
swap again ]
|
|
drop ] is hailstone ( n --> [ )
|
|
|
|
[ stack ] is longest ( --> s )
|
|
|
|
[ stack ] is length ( --> s )
|
|
|
|
27 hailstone
|
|
say "The hailstone sequence for 27 has "
|
|
dup size echo say " elements." cr
|
|
say "It starts with"
|
|
dup 4 split drop witheach [ sp echo ]
|
|
say " and ends with"
|
|
-4 split nip witheach [ sp echo ]
|
|
say "." cr cr
|
|
|
|
0 longest put 0 length put
|
|
99999 times
|
|
[ i^ 1+ hailstone size
|
|
dup length share > if
|
|
[ dup length replace
|
|
i^ 1+ longest replace ]
|
|
drop ]
|
|
longest take echo
|
|
say " has the longest sequence of any number less than 100000."
|
|
cr say "It is " length take echo say " elements long." cr
|