Advertisement
MGakowski

Collatz Sequence Term Analyzer

Feb 9th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. __author__ = 'Cyph3r'
  2.  
  3. N = 10
  4. X = N
  5. stepCount = 1
  6. highestS = 0
  7. highestN = 0
  8. g = 0
  9.  
  10. f = str(input("Please name new file: "))
  11. g = int(input("Limit to calculate to? "))
  12. text_file0 = open(f+".txt", "w")
  13.  
  14. while N != g+1:
  15.     if X == 1:
  16.         text_file0.write(str(N)+":"+str(stepCount)+"\n")
  17.         print(str(N)+" : "+str(stepCount))
  18.         if stepCount > highestS:
  19.             print("* "+str(N)+" : "+str(stepCount))
  20.             highestS = stepCount
  21.             highestN = N
  22.         stepCount = 1
  23.         N += 1
  24.         X = N
  25.     else:
  26.         if X % 2 == 0:
  27.             X /= 2
  28.             stepCount += 1
  29.         else:
  30.             X = X * 3 + 1
  31.             stepCount += 1
  32. else:
  33.     print("Largest Collatz sequence with most terms = "+str(highestN)+" with "+str(highestS)+" terms!")
  34.     text_file0.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement