Function Call Counter
Python
Medium
2 views
Problem Description
Read n. Define function step(x) that returns x//2 if x even else 3*x+1. Starting from n, keep applying step until 1. Output number of steps.
Input Format
One integer n.
Output Format
One integer steps.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=int(s)
def step(x):
return x//2 if x%2==0 else 3*x+1
cnt=0
x=n
while x!=1:
x=step(x)
cnt+=1
sys.stdout.write(str(cnt))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!