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())
for i in range(9999):
if(n==1):
print(i)
break;
if(n%2==0):
n/=2
else:
n=n*3+1
Comments
Leave a comment