Skip to content

Instantly share code, notes, and snippets.

@righ1113
Last active February 6, 2021 22:57
Show Gist options
  • Save righ1113/a0770ca1f29c1d34fcdbe3315f7c8ef7 to your computer and use it in GitHub Desktop.
Save righ1113/a0770ca1f29c1d34fcdbe3315f7c8ef7 to your computer and use it in GitHub Desktop.
Forth でコラッツ
\ s" collatz.f" included
: m3p1 ( n -- n' )
3 * 1 + ;
: choice ( n b -- n' )
1 = if m3p1 else 2 / then ; \ 比較対象は消える
: myMod2 ( n -- n n b )
dup dup 2 mod ;
: collatz ( n -- n a b .. 1 )
recursive dup 1 = if else myMod2 choice collatz then ;
: dangerAllOut ( .. a b -- )
recursive . dangerAllOut ; \ 関数合成じゃないよ
: safeAllOut ( .. a b -- )
recursive depth 0 = if else . safeAllOut then ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment