- def collatz(n)
- print "#{n} "
- if n > 1
- if n % 2 != 0
- collatz(3*n + 1)
- else
- collatz(n/2)
- end
- end
- end
- #Example
- collatz(21) #=> 21 64 32 16 8 4 2 1
- #该片段来自于http://www.codesnippet.cn/detail/250420133029.html
来源: http://www.codesnippet.cn/detail/250420133029.html