Advertisement
Guest User

Pedro Tome

a guest
Apr 16th, 2010
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.48 KB | None | 0 0
  1. #
  2. # The Collatz Conjecture Buster (not)
  3. # version 1.1 (added {Export as Excel feature} and {Choice Validity Tests})
  4. # By Pedro Tome'
  5. #
  6. # This script needs the pyExcelerator module,
  7. # available at http://sourceforge.net/projects/pyexcelerator/
  8. #
  9.  
  10.  
  11. # Setting variables:
  12. while True: # Minimum number validity test
  13.     try:
  14.         min_num = int(raw_input("Minimum number: "))
  15.         if min_num < 2:
  16.             print "Error: must be a number equal or higher than 2!"
  17.             continue
  18.         else:
  19.             break
  20.     except ValueError:
  21.         print "Error: must be a number equal or higher than 2!"
  22.         continue
  23.  
  24.  
  25. while True: # Maximum number validity test
  26.     try:
  27.         max_num = int(raw_input("Maximum number: "))
  28.         if max_num < min_num:
  29.             print "Error: must be equal or higher than the maximum number %i!" % (min_num)
  30.             continue
  31.         else:
  32.             break    
  33.     except ValueError:
  34.         print "Error: must be a number equal or higher than the maximum number %i!" % (min_num)
  35.         continue      
  36.  
  37.  
  38. while True: # Screen printing choice validity test    
  39.     print_choice = raw_input("Print all numbers on the screen? (faster if you choose not to) [1 or 0] ") # Printing everything makes the process slower...
  40.     if print_choice != "0" and print_choice != "1":
  41.         print "Error: must be 0 or 1"
  42.         continue
  43.     else:
  44.         print_choice = int(print_choice)
  45.         break
  46.  
  47.    
  48. while True: # Excel exporting choice validity test
  49.     excel_choice = raw_input("Export as an Excel spreadsheet? [1 or 0] ")
  50.     if excel_choice != "0" and excel_choice != "1":
  51.         print "Error: must be 0 or 1"
  52.         continue
  53.     else:
  54.         excel_choice = int(excel_choice)
  55.         break
  56.  
  57.  
  58. a = min_num
  59. loop_num = 0
  60. found_num = False
  61. #
  62.  
  63. if excel_choice == 1:
  64.     from pyExcelerator import *
  65.     wb = Workbook()
  66.     ws0 = wb.add_sheet("0")
  67.  
  68.     excel_filename = raw_input("Excel spreadsheet filename: ")
  69.     ws0.write(0,0,"Number")
  70.     ws0.write(0,1,"Steps")
  71.  
  72.     y = 1
  73.        
  74.     # The actual loop/calculator:
  75.     if max_num - min_num >= 20000:
  76.         print "Calculating..."
  77.     else:
  78.         pass
  79.    
  80.     while a <= max_num:
  81.         b = a
  82.    
  83.         while b != 1:
  84.             if b%2 == 0: # If b is even (b%2 = remainder of b/2)
  85.                 b = b/2
  86.             else: # If b is odd
  87.                 b = 3*b+1
  88.             loop_num += 1
  89.  
  90.         # Printing on the Excel spreadsheet
  91.         ws0.write(y,0,a)
  92.         ws0.write(y,1,loop_num)
  93.         y += 1
  94.         #
  95.        
  96.         if b < 1:
  97.             print "Exception found! Number %i!" % (a) # This is obviously impossible... :(
  98.             raw_input("Press ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
  99.             found_num = True
  100.             break
  101.         else:
  102.             if print_choice == 1:
  103.                 print "%i - %i steps" %(a,loop_num)
  104.             loop_num = 0 # loop_num must be reset so loop_num(a) doesn't add with loop_num(a+1)
  105.             a += 1
  106.            
  107.     if found_num == False:
  108.         print "Done calculating."
  109.         print "\nExporting to Excel..."
  110.         wb.save(excel_filename+".xls")
  111.         print "Done."
  112.         raw_input("\nPress ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
  113.     #
  114.    
  115. else:
  116.     # The actual loop/calculator:
  117.     if max_num - min_num >= 20000:
  118.         print "Calculating..."
  119.     else:
  120.         pass
  121.        
  122.     while a <= max_num:
  123.         b = a
  124.    
  125.         while b != 1:
  126.             if b%2 == 0: # If b is even (b%2 = remainder of b/2)
  127.                 b = b/2
  128.             else: # If b is odd
  129.                 b = 3*b+1
  130.             loop_num += 1
  131.        
  132.         if b < 1:
  133.             print "Exception found! Number %i!" % (a) # This is obviously impossible... :(
  134.             raw_input("Press ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
  135.             found_num = True
  136.             break
  137.         else:
  138.             if print_choice == 1:
  139.                 print "%i - %i steps" %(a,loop_num)
  140.             loop_num = 0 # loop_num must be reset so loop_num(a) doesn't add with loop_num(a+1)
  141.             a += 1
  142.  
  143.     if found_num == False:
  144.         print "Done calculating."
  145.         raw_input("\nPress ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
  146.     #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement