amloessb

hailstone.py (w/ dictionary)

Jan 24th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import pickle
  3. import os
  4.  
  5. hailrecord = {1:0, 2:1}
  6.  
  7. def is_int(s):
  8.     try:
  9.         int(s)
  10.         return True
  11.     except ValueError:
  12.         return False
  13.  
  14. def hailstone(n, prt, opt):
  15.     if n < 1:
  16.         print "Pick a positive number, dummy."
  17.         return -1
  18.     elif not is_int(n):
  19.         print "That's not a number, dummy."
  20.         return -1
  21.     else:
  22.         n = int(n)
  23.         steps = 0
  24.         while n <> 1:
  25.             if opt:
  26.                 if n in hailrecord:
  27.                     return hailrecord[n] + steps
  28.             if prt: print n,
  29.             steps += 1
  30.             if n % 2 == 0:
  31.                 n = n / 2
  32.             else:
  33.                 n = n * 3 + 1
  34.         if prt: print n
  35.         return steps
  36.  
  37. if __name__ == '__main__':
  38.     if (len(sys.argv) < 2 or len(sys.argv) > 3):
  39.         print "You done goofed!"
  40.         sys.exit(1)
  41.     else:
  42.         if sys.argv[1] == "find":
  43.             goal = int(sys.argv[2])
  44.             if not is_int(goal):
  45.                 print "That goal's not a number, dummy."
  46.                 sys.exit(1)
  47.             else:
  48.                 if not os.path.exists("hail.pck"):
  49.                     with open("hail.pck", "w") as file:
  50.                         pickle.dump(hailrecord, file)
  51.                         print "Created new dictionary."
  52.                 else:
  53.                     size = os.path.getsize("hail.pck")
  54.                     print "Loading dictionary:",
  55.                     print "%.2f" % (size / 1048576.0), "MiB"
  56.                     with open("hail.pck", "r") as file:
  57.                         hailrecord = pickle.load(file)
  58.                         print "Dictionary loaded."
  59.                 num = 2
  60.                 s = 0
  61.                 while s < goal:
  62.                     num += 1
  63.                     if num in hailrecord:
  64.                         temp = hailrecord[num]
  65.                     else:
  66.                         temp = hailstone(num, False, True)
  67.                         hailrecord[num] = temp
  68.                     if temp > 0 and temp > s:
  69.                         s = temp
  70.                     elif temp < 0:
  71.                         sys.exit(2)
  72.                 hailstone(num, True, False)
  73.                 print num, "has a hailstone sequence of length", s
  74.                 print "Saving dictionary:",
  75.                 print "%.3f" % (len(hailrecord) / 1000.0), "K entries"
  76.                 with open("hail.pck", "w") as file:
  77.                     pickle.dump(hailrecord, file)
  78.                     print "Dictionary saved."
  79.         else:
  80.             num = sys.argv[1]
  81.             hailstone(num, True, False)
Advertisement
Add Comment
Please, Sign In to add comment