Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pickle_attempt_for_web.py
- import pickle
- 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:
- return factors
- factors[-1] = int(factors[-1]) # without this some last factors are floats, ending in .0
- return factors
- D = {}
- f = open("factors.txt", 'rb')
- data = pickle.load(f)
- f.close
- D = data
- begin = 4000000
- w = 10
- end = begin + w
- for n in range(begin, end):
- factors_list = factorsOfInteger(n)
- D[n] = factors_list
- print()
- while True:
- n = int(input("integer: "))
- if n == 0:
- print("D has", len(D), "entries")
- f = open("factors.txt", 'ab')
- pickle.dump(D, f)
- f.close
- break
- try:
- factors = D[n]
- print("the factors of", n, "are", factors)
- except KeyError:
- factors = factorsOfInteger(n)
- print("the factors of", n, "are", factors)
- D[n] = factors
- print(D.items())
- print()
- print("D has", len(D), "entries")
- #===================OUTPUTS using lines 54 through 59=======================
- # after adding items 4, 1000000-1000009 and 2000000-2000009
- #integer: 4
- #the factors of 4 are [2, 2]
- #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]),
- #(1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(2000000, [2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(2000007, [3, 3, 61, 3643]),
- #(2000002, [2, 101, 9901]),
- #(2000001, [3, 666667]),
- #(2000008, [2, 2, 2, 53, 53, 89]),
- #(2000003, [2000003]),
- #(4, [2, 2]),
- #(2000009, [11, 11, 16529]),
- #(2000004, [2, 2, 3, 166667]),
- #(2000006, [2, 1000003]),
- #(2000005, [5, 7, 57143])])
- #D has 21 entries
- #integer: 0
- #D has 21 entries
- #===================OUTPUTS using lines 54 through 59=======================
- # after adding items 3000000-3000009, but notice items 2000000-2000009 are missing!
- #integer: 4
- #the factors of 4 are [2, 2]
- #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]),
- #(1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(3000007, [17, 109, 1619]),
- #(3000002, [2, 557, 2693]),
- #(3000006, [2, 3, 3, 166667]),
- #(3000008, [2, 2, 2, 11, 73, 467]),
- #(3000003, [3, 101, 9901]),
- #(3000009, [3, 1000003]),
- #(3000004, [2, 2, 7, 307, 349]),
- #(3000001, [853, 3517]),
- #(3000000, [2, 2, 2, 2, 2, 2, 3, 5, 5, 5, 5, 5, 5]),
- #(4, [2, 2]), (3000005, [5, 19, 23, 1373])])
- #D has 21 entries
- #integer: 0
- #D has 21 entries
- #===================OUTPUTS using lines 54 through 59=======================
- # after adding items 4000000-4000009, but notice items 3000000-3000009 are now missing!
- #integer: 4
- #the factors of 4 are [2, 2]
- #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]),
- #(1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(4000007, [11, 79, 4603]),
- #(4000001, [41, 97561]),
- #(4000002, [2, 3, 666667]),
- #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(4000008, [2, 2, 2, 3, 166667]),
- #(4000003, [7, 139, 4111]), (4000009, [13, 307693]),
- #(4000004, [2, 2, 101, 9901]),
- #(4000006, [2, 2000003]), (4, [2, 2]),
- #(4000005, [3, 3, 5, 103, 863])])
- #D has 21 entries
- #integer: 0
- #D has 21 entries
- #=======OUTPUTS using only lines 78 and 80 to add items 100, 101, 103======
- #integer: 100
- #the factors of 100 are [2, 2, 5, 5]
- #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]),
- #(1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(4000007, [11, 79, 4603]),
- #(4000001, [41, 97561]),
- #(4000002, [2, 3, 666667]),
- #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(4000008, [2, 2, 2, 3, 166667]),
- #(4000003, [7, 139, 4111]),
- #(4000009, [13, 307693]),
- #(100, [2, 2, 5, 5]),
- #(4000004, [2, 2, 101, 9901]),
- #(4000006, [2, 2000003]),
- #(4000005, [3, 3, 5, 103, 863])])
- #D has 21 entries
- #integer: 101
- #the factors of 101 are [101]
- #dict_items([(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(4000001, [41, 97561]),
- #(4000002, [2, 3, 666667]),
- #(4000003, [7, 139, 4111]),
- #(4000004, [2, 2, 101, 9901]),
- #(4000005, [3, 3, 5, 103, 863]),
- #(4000006, [2, 2000003]),
- #(4000007, [11, 79, 4603]),
- #(4000008, [2, 2, 2, 3, 166667]),
- #(4000009, [13, 307693]),
- #(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]),
- #(1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(100, [2, 2, 5, 5]),
- #(101, [101])])
- #D has 22 entries
- #integer: 103
- #the factors of 103 are [103]
- #dict_items([(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(4000001, [41, 97561]),
- #(4000002, [2, 3, 666667]),
- #(4000003, [7, 139, 4111]),
- #(4000004, [2, 2, 101, 9901]),
- #(4000005, [3, 3, 5, 103, 863]),
- #(4000006, [2, 2000003]),
- #(4000007, [11, 79, 4603]),
- #(4000008, [2, 2, 2, 3, 166667]),
- #(4000009, [13, 307693]),
- #(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]), (1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(100, [2, 2, 5, 5]),
- #(101, [101]),
- #(103, [103])])
- #D has 23 entries
- #integer: 0
- #D has 23 entries
- #==============OUTPUT AFTER RESTARTING PROGRAM======
- # Items 100, 101, and 103 are missing!
- #integer: 4
- #the factors of 4 are [2, 2]
- #dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(1000001, [101, 9901]),
- #(1000002, [2, 3, 166667]),
- #(1000003, [1000003]),
- #(1000004, [2, 2, 53, 53, 89]),
- #(1000005, [3, 5, 163, 409]),
- #(1000006, [2, 7, 71429]),
- #(1000007, [29, 34483]),
- #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]),
- #(1000009, [293, 3413]),
- #(4000007, [11, 79, 4603]),
- #(4000001, [41, 97561]),
- #(4000002, [2, 3, 666667]),
- #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]),
- #(4000008, [2, 2, 2, 3, 166667]),
- #(4000003, [7, 139, 4111]),
- #(4000009, [13, 307693]),
- #(4000004, [2, 2, 101, 9901]),
- #(4000006, [2, 2000003]),
- #(4, [2, 2]),
- #(4000005, [3, 3, 5, 103, 863])])
- #D has 21 entries
- #integer: 0
- #D has 21 entries
- #===================END OF OUTPUTS=======================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement