Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Source: 3nplus1.py
- # Author: Jeffrey Cartagena
- # Date: April, 29, 2009
- # readFile function
- def readFile(inFile):
- line = inFile.readline().rstrip()
- if not line:
- return 0
- nums = line.split()
- return nums
- # End of readFile function
- # iterations function
- def iterations(value):
- count = 1
- while value != 1:
- if value % 2 == 0:
- value = value / 2
- else:
- value = value * 3 + 1
- count += 1
- return count
- # End of iterations function
- # This function call the function iterations to check
- # the iterations of each number then compare them tu return the maximum
- def checkIterations(flag):
- intMax = 1
- for i in range(int(flag[0]), int(flag[1])):
- temp = iterations(i)
- if temp > intMax:
- intMax = temp
- return intMax
- # End of checkIterations
- # writeFile function
- def writeFile(flag, intMax, outFile):
- outFile.write(str(flag[0]) + " " + str(flag[1]) + " " + str(intMax) + '\n')
- # End of writeFile function
- # Main Routine
- try:
- # Opening the files input.in, output.out
- inFile = open("input.in")
- outFile = open("output.out", 'w')
- except IOError:
- raw_input("Files cannot be opened\n")
- exit
- except:
- raw_input("Something unexpected happened\n")
- exit
- # Variable Declaration
- flag = readFile(inFile)
- while flag:
- intMax = checkIterations(flag)
- writeFile(flag, intMax, outFile)
- flag = readFile(inFile)
- inFile.close()
- outFile.close()
- raw_input("Finishing the problem\n")
- # End of Main Routine
Advertisement
Add Comment
Please, Sign In to add comment