#P323. Collatz(n)

Collatz(n)

題目描述

Write a function Collatz(n) which takes a positive integer n as input, and count the number of steps until the number reaches 1 by the following procedure:

  • If n is even, divide n by 2 (n -> n/2)
  • If n is odd, multiple n by 3 and then add 1 to the product (n -> 3n+1)

輸入格式

A positive integer n.

輸出格式

A positive integer, the number of steps for n to reach 1.

Samples

3
7
6
8
9
19
16
4
27
111

原始資料

  • Zero1 題號:a323
  • Hydro 題號:Z0323
  • Locale:zh_TW
  • Display:practice