Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active December 18, 2015 17:09
Show Gist options
  • Save chomado/5816618 to your computer and use it in GitHub Desktop.
Save chomado/5816618 to your computer and use it in GitHub Desktop.
コラッツの数列
collatz :: Int -> [Int]
collatz 1 = [1]
collatz n
| n `mod` 2 == 0 = n : collatz ( n `div` 2 )
| otherwise = n : collatz ( 3 * n + 1 )
-- って書いたら、even, odd っていう便利なものがありました。書き直します
collatz':: Int -> [Int]
collatz' 1 = [1]
collatz' n
| even n = n : collatz' ( n `div` 2 )
| odd n = n : collatz' ( 3 * n + 1 )
-- 実行例
-- > collatz 30
-- [30,15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment