Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # The Collatz Conjecture Buster (not)
- # version 1.1 (added {Export as Excel feature} and {Choice Validity Tests})
- # By Pedro Tome'
- #
- # This script needs the pyExcelerator module,
- # available at http://sourceforge.net/projects/pyexcelerator/
- #
- # Setting variables:
- while True: # Minimum number validity test
- try:
- min_num = int(raw_input("Minimum number: "))
- if min_num < 2:
- print "Error: must be a number equal or higher than 2!"
- continue
- else:
- break
- except ValueError:
- print "Error: must be a number equal or higher than 2!"
- continue
- while True: # Maximum number validity test
- try:
- max_num = int(raw_input("Maximum number: "))
- if max_num < min_num:
- print "Error: must be equal or higher than the maximum number %i!" % (min_num)
- continue
- else:
- break
- except ValueError:
- print "Error: must be a number equal or higher than the maximum number %i!" % (min_num)
- continue
- while True: # Screen printing choice validity test
- 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...
- if print_choice != "0" and print_choice != "1":
- print "Error: must be 0 or 1"
- continue
- else:
- print_choice = int(print_choice)
- break
- while True: # Excel exporting choice validity test
- excel_choice = raw_input("Export as an Excel spreadsheet? [1 or 0] ")
- if excel_choice != "0" and excel_choice != "1":
- print "Error: must be 0 or 1"
- continue
- else:
- excel_choice = int(excel_choice)
- break
- a = min_num
- loop_num = 0
- found_num = False
- #
- if excel_choice == 1:
- from pyExcelerator import *
- wb = Workbook()
- ws0 = wb.add_sheet("0")
- excel_filename = raw_input("Excel spreadsheet filename: ")
- ws0.write(0,0,"Number")
- ws0.write(0,1,"Steps")
- y = 1
- # The actual loop/calculator:
- if max_num - min_num >= 20000:
- print "Calculating..."
- else:
- pass
- while a <= max_num:
- b = a
- while b != 1:
- if b%2 == 0: # If b is even (b%2 = remainder of b/2)
- b = b/2
- else: # If b is odd
- b = 3*b+1
- loop_num += 1
- # Printing on the Excel spreadsheet
- ws0.write(y,0,a)
- ws0.write(y,1,loop_num)
- y += 1
- #
- if b < 1:
- print "Exception found! Number %i!" % (a) # This is obviously impossible... :(
- raw_input("Press ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
- found_num = True
- break
- else:
- if print_choice == 1:
- print "%i - %i steps" %(a,loop_num)
- loop_num = 0 # loop_num must be reset so loop_num(a) doesn't add with loop_num(a+1)
- a += 1
- if found_num == False:
- print "Done calculating."
- print "\nExporting to Excel..."
- wb.save(excel_filename+".xls")
- print "Done."
- raw_input("\nPress ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
- #
- else:
- # The actual loop/calculator:
- if max_num - min_num >= 20000:
- print "Calculating..."
- else:
- pass
- while a <= max_num:
- b = a
- while b != 1:
- if b%2 == 0: # If b is even (b%2 = remainder of b/2)
- b = b/2
- else: # If b is odd
- b = 3*b+1
- loop_num += 1
- if b < 1:
- print "Exception found! Number %i!" % (a) # This is obviously impossible... :(
- raw_input("Press ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
- found_num = True
- break
- else:
- if print_choice == 1:
- print "%i - %i steps" %(a,loop_num)
- loop_num = 0 # loop_num must be reset so loop_num(a) doesn't add with loop_num(a+1)
- a += 1
- if found_num == False:
- print "Done calculating."
- raw_input("\nPress ENTER to exit.") # Must use this so the program doesn't close immediately after finishing on Windowze CMD
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement