Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pickle_attempt_for_web2.py
- from time import time as t
- import pickle
- # Should gmpy2 be unavailable, just remark out the highlighted lines.
- import gmpy2
- import random
- def int_commas(n):
- """
- inserts commas into integers and returns the string
- E.g. -12345678 -> -12,345,789
- """
- return format(n, ',d')
- def printTime(timeStart, timeEnd):
- from mycalc import hmsToText
- timeElapsed = timeEnd - timeStart
- if timeElapsed > 60:
- print("Time was", hmsToText(timeElapsed))
- else:
- print("Time was %.4g seconds" % timeElapsed)
- def randIntOfGivenLength(length):
- """
- Return a random int of a given number of digits
- """
- return random.randint(10**(length-1), 10**length - 1)
- def findFactor(n, startingAt):
- """
- Written by Kent Johnson
- """
- limit = int(n**.5) + 1
- x = startingAt
- while x < limit:
- if not n % x:
- return x
- x += 2
- return False
- def factorsOfInteger(n):
- """
- Return list of prime factors of n.
- Returns [n] if n is prime.
- Re-written largely by Kent Johnson
- """
- factors = []
- if n == 1:
- return [1]
- # Get the factors of two out of the way to simplify findFactor
- while n % 2 == 0:
- factors.append(2)
- n = n // 2
- lastFactor = 3
- r = n
- while True:
- factor = findFactor(r, lastFactor)
- if not factor:
- # r is prime
- if r == 1: # This is needed, at least for powers of 2
- break
- factors.append(r)
- break
- factors.append(factor)
- lastFactor = factor
- r = r / factor
- if len(factors) == 1: # changed this from if n in factors
- return factors
- factors[-1] = int(factors[-1]) # without this some last factors are floats, ending in .0
- return factors
- D = {}
- f = open("factors.dat", 'rb')
- data = pickle.load(f)
- f.close
- D = data
- begin = 2000000000
- w = 1000
- end = begin + w
- add_flag = 0
- if add_flag == 1:
- t0 = t()
- for n in range(begin, end):
- factors_list = factorsOfInteger(n)
- D[n] = factors_list
- t1 = t()
- printTime(t0,t1)
- print()
- while True:
- oldnew_flag = 0
- print("Enter a positive integer OR")
- print("Enter nothing to generate an 18-digit integer OR")
- print("Enter a zero to close the program.")
- print()
- s = input("> ")
- if "10**" in s:
- n = 10**(int(s[4:]))
- plus = input("plus: ")
- if plus == '':
- plus = 0
- else:
- plus = int(plus)
- n += plus
- elif s != '':
- n = int(s)
- if n >= 10**19:
- print(n, "is too big: calculation might take too long.")
- print("Choose another n")
- print()
- continue
- elif gmpy2.is_prime(n) and len(str(n)) >= 18:
- print(n, "is prime -- will take too long.")
- print("Choose another n")
- print()
- continue
- elif s == '':
- n = randIntOfGivenLength(18)
- print("n =", n, "a random 18-digit integer")
- if gmpy2.is_prime(n):
- print(n, "is prime -- will take too long.")
- print("Choose another n")
- print()
- continue
- if n == 0:
- print("D has", int_commas(len(D)), "entries")
- f = open("factors.dat", 'wb')
- pickle.dump(D, f)
- f.close
- break
- t0 = t()
- try:
- factors = D[n]
- print("the factors of", int_commas(n), "are", factors)
- oldnew_flag = 1
- print("OLD")
- except KeyError:
- factors = factorsOfInteger(n)
- print("the factors of", int_commas(n), "are", factors)
- D[n] = factors
- oldnew_flag = 2
- print("NEW")
- t1 = t()
- print()
- #print(D.items())
- #print(D.keys())
- printTime(t0,t1)
- if oldnew_flag == 1:
- print("OLD ENTRY")
- elif oldnew_flag == 2:
- print("NEW ENTRY")
- print("D has", int_commas(len(D)), "entries")
- print()
- #=========Output of one session==============
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #>
- #n = 279918016070854658 a random 18-digit integer
- #the factors of 279,918,016,070,854,658 are [2, 7, 41, 487662048903928]
- #NEW
- #Time was 4.13 seconds
- #NEW ENTRY
- #D has 2 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 1234
- #the factors of 1,234 are [2, 617]
- #NEW
- #Time was 0.0009999 seconds
- #NEW ENTRY
- #D has 3 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 3456
- #the factors of 3,456 are [2, 2, 2, 2, 2, 2, 2, 3, 3, 3]
- #NEW
- #Time was 0 seconds
- #NEW ENTRY
- #D has 4 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 3456
- #the factors of 3,456 are [2, 2, 2, 2, 2, 2, 2, 3, 3, 3]
- #OLD
- #Time was 0.001 seconds
- #OLD ENTRY
- #D has 4 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #>
- #n = 668979809228717177 a random 18-digit integer
- #the factors of 668,979,809,228,717,177 are [2767, 111031, 2177507201]
- #NEW
- #Time was 0.027 seconds
- #NEW ENTRY
- #D has 5 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 0
- #D has 5 entries
- #===============Output of a new session================
- # added 1000 new entries from 2000000000 to 2000001000 using lines 83-90
- #Time was 1.277 seconds
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 0
- #D has 1,005 entries
- #===============Output of a new session================
- # I set the add_flag to 0 (line 83) so lines 83-90 will be ignored
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 8756765
- #the factors of 8,756,765 are [5, 1751353]
- #NEW
- #Time was 0.0009999 seconds
- #NEW ENTRY
- #D has 1,006 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #>
- #n = 158970453260687288 a random 18-digit integer
- #the factors of 158,970,453,260,687,288 are [2, 2, 2, 11, 67, 157, 251, 6397, 106957]
- #NEW
- #Time was 0.002 seconds
- #NEW ENTRY
- #D has 1,007 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 1111111111111
- #the factors of 1,111,111,111,111 are [53, 79, 265371653]
- #NEW
- #Time was 0.01 seconds
- #NEW ENTRY
- #D has 1,008 entries
- #Enter a positive integer OR
- #Enter nothing to generate an 18-digit integer OR
- #Enter a zero to close the program.
- #> 0
- #D has 1,008 entries
- #==================END=========================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement