Last active
December 18, 2015 17:09
-
-
Save chomado/5816618 to your computer and use it in GitHub Desktop.
コラッツの数列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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