Collatz Steps
Python
Medium
3 views
Problem Description
Read integer n (>0). Do: if n is even n=n/2 else n=3n+1. Count steps until n becomes 1. Output 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)
steps=0
while n!=1:
if n%2==0:
n//=2
else:
n=3*n+1
steps+=1
sys.stdout.write(str(steps))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!