Python Problem Solving
You're given a positive integer: Perform the sequence of operations as per below guidelines until the value reaches 1:
If N is even --> N / 2
If N is odd --> N * 3 + 1
Print the total number of steps it took to reach 1.
Input
The first line of input contains an integer.
Output
The output should be a single integer denoting the total number of steps.
Explanation
For Example, using N = 10 as the input, with 6 steps.
10 is even - 10/2 = 5
5 is odd - 5 * 3 + 1 = 16
16 is even - 16 / 2 = 8
8 is even - 8 /2 = 4
4 is even - 4/2 = 2
2 is even - 2/2 = 1 --> Reached 1.
So the output should be 6.
Sample Input1
10
Sample Output1
6
Sample Input2
345
Sample Output2
125
N=int(input())
step=0
while N!=1:
if N%2==0:
N=N/2
step+=1
else:
N=N*3+1
step+=1
print(step)
Comments
Leave a comment