#P0016. 3N+1 问题

3N+1 问题

1. Description

This is a well-known question in mathematics. Start with an integer num greater than 1, update num's value as follow:

  • if num is odd, num = 3 * num + 1
  • if num is even, num = num // 2

Keep updating num, it will finally be 1. In this problem, we'll print how num goes to 1 from initial value.

# Example:
num = 30
# Output:
30 -> 15 -> 46 -> 23 -> 70 -> 35 -> 106 -> 53 -> 160 -> 80 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1

Try some integers to validate if all of them finally become 1.

2. Input & Output

Input:

  • An integer
  • Use input() as input. Notice that the value you get from input() is a string

Output:

  • Use print() for your output
  • Use a string to demonstrate how the number finally become 1. The numbers are separated by arrow ->
  • Note there's a space between arrows and numbers

Example

# Input: 
1
# Output: 
1
# Input: 
3
# Output: 
3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1