I am trying to get into the habit of solving programming puzzles in my spare time but am stuck on the first one (which I know can be for a large number of reasons, hence my posting for help). Shown below is my code that is meant to solve the 3n+1 Challenge:
from sys import stdin def main(): curr_Line=stdin.readline() while curr_Line!=None: curr_Line=curr_Line.split() min_num=int(curr_Line) max_num=int(curr_Line) maxCycleLength =0 for curr_num in range(min_num,max_num+1): currCycleLength =1 while curr_num!=1: currCycleLength +=1 if curr_num%2==0: curr_num=curr_num/2 else: curr_num=3*curr_num+1 maxCycleLength=max(currCycleLength,maxCycleLength) print(min_num,max_num,maxCycleLength,sep=" ") curr_Line=stdin.readline() return main()
When I run this, the UVA online judge says that I've exceeded the 3 second time limit.
I've tried the following:
- changing the the
for line in stdin:
Any suggestions would be much appreciated.