Weird Algorithm
Problem Source
Problem Statement
Print the sequence of integers by following the rules:
- If the number is even, divide it by $2$.
- If the number is odd, multiply it by $3$ and add $1$.
- Repeat this process until the number becomes $1$.
- The first number is $n$.
Solution
1
2
3
4
5
6
7
n = int(input())
while n > 1:
print(n, end=" ")
n = (n * 3 + 1) if n % 2 else n // 2
print(1)
This post is licensed under CC BY 4.0 by the author.