View difference between Paste ID: SNwKRuSK and s9QzYZ3a
SHOW: | | - or go back to the newest paste.
1
# pickle_attempt_for_web.py
2
3
import pickle
4
5
def findFactor(n, startingAt):
6
    """
7
    Written by Kent Johnson
8
    """
9
    limit = int(n**.5) + 1
10
    x = startingAt
11
    while x < limit:
12
        if not n % x:
13
            return x
14
        x += 2
15
    return False
16
17
def factorsOfInteger(n):
18
    """
19
    Return list of prime factors of n.
20
    Returns [n] if n is prime.
21
    Re-written largely by Kent Johnson
22
    """
23
    factors = []
24
    if n == 1:
25
        return [1]
26
    # Get the factors of two out of the way to simplify findFactor
27
    while n % 2 == 0:
28
        factors.append(2)
29
        n = n // 2
30
    lastFactor = 3
31
    r = n
32
    while True:
33
        factor = findFactor(r, lastFactor)
34
        if not factor:
35
            # r is prime
36
            if r == 1: # This is needed, at least for powers of 2
37
                break
38
            factors.append(r)
39
            break
40
        factors.append(factor)
41
        lastFactor = factor
42
        r = r / factor
43
    if len(factors) == 1: 
44
        return factors
45
    factors[-1] = int(factors[-1]) # without this some last factors are floats, ending in .0
46
    return factors
47
48
D = {}
49
f = open("factors.txt", 'rb')
50
data = pickle.load(f)
51
f.close
52
D = data
53
        
54
begin = 4000000
55
w = 10
56
end = begin + w
57
for n in range(begin, end):
58
    factors_list = factorsOfInteger(n)
59
    D[n] = factors_list
60
61
print()
62
63
while True:
64
    n = int(input("integer: "))
65
            
66
    if n == 0:
67
        print("D has", len(D), "entries")
68
        f = open("factors.txt", 'ab')
69
        pickle.dump(D, f)
70
        f.close
71
        break
72
    
73
    try:
74
        factors = D[n]
75
        print("the factors of", n, "are", factors)
76
        
77
    except KeyError:
78
        factors = factorsOfInteger(n)
79
        print("the factors of", n, "are", factors)
80
        D[n] = factors
81
        
82
    print(D.items())
83
    print()
84
    print("D has", len(D), "entries")
85
    
86
#===================OUTPUTS using lines 54 through 59=======================
87
# after adding items 4, 1000000-1000009 and 2000000-2000009
88
#integer: 4
89
#the factors of 4 are [2, 2]
90
#dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
91
            #(1000001, [101, 9901]), 
92
            #(1000002, [2, 3, 166667]), 
93
            #(1000003, [1000003]), 
94
            #(1000004, [2, 2, 53, 53, 89]), 
95
            #(1000005, [3, 5, 163, 409]), 
96
            #(1000006, [2, 7, 71429]), 
97
            #(1000007, [29, 34483]), 
98
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
99
            #(1000009, [293, 3413]), 
100
            
101
            #(2000000, [2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
102
            #(2000007, [3, 3, 61, 3643]), 
103
            #(2000002, [2, 101, 9901]), 
104
            #(2000001, [3, 666667]), 
105
            #(2000008, [2, 2, 2, 53, 53, 89]), 
106
            #(2000003, [2000003]), 
107
            #(4, [2, 2]), 
108
            #(2000009, [11, 11, 16529]), 
109
            #(2000004, [2, 2, 3, 166667]), 
110
            #(2000006, [2, 1000003]), 
111
            #(2000005, [5, 7, 57143])])
112
113
#D has 21 entries
114
#integer: 0
115
#D has 21 entries
116
#===================OUTPUTS using lines 54 through 59=======================
117
# after adding items 3000000-3000009, but notice items 2000000-2000009 are missing!
118
119
#integer: 4
120
#the factors of 4 are [2, 2]
121
#dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
122
            #(1000001, [101, 9901]), 
123
            #(1000002, [2, 3, 166667]), 
124
            #(1000003, [1000003]), 
125
            #(1000004, [2, 2, 53, 53, 89]), 
126
            #(1000005, [3, 5, 163, 409]), 
127
            #(1000006, [2, 7, 71429]), 
128
            #(1000007, [29, 34483]), 
129
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
130
            #(1000009, [293, 3413]), 
131
            
132
            #(3000007, [17, 109, 1619]), 
133
            #(3000002, [2, 557, 2693]), 
134
            #(3000006, [2, 3, 3, 166667]), 
135
            #(3000008, [2, 2, 2, 11, 73, 467]), 
136
            #(3000003, [3, 101, 9901]), 
137
            #(3000009, [3, 1000003]), 
138
            #(3000004, [2, 2, 7, 307, 349]), 
139
            #(3000001, [853, 3517]), 
140
            #(3000000, [2, 2, 2, 2, 2, 2, 3, 5, 5, 5, 5, 5, 5]), 
141
            #(4, [2, 2]), (3000005, [5, 19, 23, 1373])])
142
143
#D has 21 entries
144
#integer: 0
145
#D has 21 entries
146
#===================OUTPUTS using lines 54 through 59=======================
147
# after adding items 4000000-4000009, but notice items 3000000-3000009 are now missing!
148
149
#integer: 4
150
#the factors of 4 are [2, 2]
151
#dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
152
            #(1000001, [101, 9901]), 
153
            #(1000002, [2, 3, 166667]), 
154
            #(1000003, [1000003]), 
155
            #(1000004, [2, 2, 53, 53, 89]), 
156
            #(1000005, [3, 5, 163, 409]), 
157
            #(1000006, [2, 7, 71429]), 
158
            #(1000007, [29, 34483]), 
159
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
160
            #(1000009, [293, 3413]), 
161
            
162
            #(4000007, [11, 79, 4603]), 
163
            #(4000001, [41, 97561]), 
164
            #(4000002, [2, 3, 666667]), 
165
            #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
166
            #(4000008, [2, 2, 2, 3, 166667]), 
167
            #(4000003, [7, 139, 4111]), (4000009, [13, 307693]), 
168
            #(4000004, [2, 2, 101, 9901]), 
169
            #(4000006, [2, 2000003]), (4, [2, 2]), 
170
            #(4000005, [3, 3, 5, 103, 863])])
171
172
#D has 21 entries
173
#integer: 0
174-
#==========OUTPUTS using only lines 78 and 80 to add items=========
174+
175
176
#=======OUTPUTS using only lines 78 and 80 to add items 100, 101, 103======
177
178
#integer: 100
179
#the factors of 100 are [2, 2, 5, 5]
180
#dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
181
            #(1000001, [101, 9901]), 
182
            #(1000002, [2, 3, 166667]), 
183
            #(1000003, [1000003]), 
184
            #(1000004, [2, 2, 53, 53, 89]), 
185
            #(1000005, [3, 5, 163, 409]), 
186
            #(1000006, [2, 7, 71429]), 
187
            #(1000007, [29, 34483]), 
188
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
189
            #(1000009, [293, 3413]), 
190
            #(4000007, [11, 79, 4603]), 
191
            #(4000001, [41, 97561]), 
192
            #(4000002, [2, 3, 666667]), 
193
            #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
194
            #(4000008, [2, 2, 2, 3, 166667]), 
195
            #(4000003, [7, 139, 4111]), 
196
            #(4000009, [13, 307693]), 
197
            #(100, [2, 2, 5, 5]), 
198
            #(4000004, [2, 2, 101, 9901]), 
199
            #(4000006, [2, 2000003]), 
200
            #(4000005, [3, 3, 5, 103, 863])])
201
202
#D has 21 entries
203
204
#integer: 101
205
#the factors of 101 are [101]
206
#dict_items([(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
207
            #(4000001, [41, 97561]), 
208
            #(4000002, [2, 3, 666667]), 
209
            #(4000003, [7, 139, 4111]), 
210
            #(4000004, [2, 2, 101, 9901]), 
211
            #(4000005, [3, 3, 5, 103, 863]), 
212
            #(4000006, [2, 2000003]), 
213
            #(4000007, [11, 79, 4603]), 
214
            #(4000008, [2, 2, 2, 3, 166667]), 
215
            #(4000009, [13, 307693]), 
216
            #(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
217
            #(1000001, [101, 9901]), 
218
            #(1000002, [2, 3, 166667]), 
219
            #(1000003, [1000003]), 
220
            #(1000004, [2, 2, 53, 53, 89]), 
221
            #(1000005, [3, 5, 163, 409]), 
222
            #(1000006, [2, 7, 71429]), 
223
            #(1000007, [29, 34483]), 
224
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
225
            #(1000009, [293, 3413]), 
226
            #(100, [2, 2, 5, 5]), 
227
            #(101, [101])])
228
229
#D has 22 entries
230
231
#integer: 103
232
#the factors of 103 are [103]
233
#dict_items([(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
234
            #(4000001, [41, 97561]), 
235
            #(4000002, [2, 3, 666667]), 
236
            #(4000003, [7, 139, 4111]), 
237
            #(4000004, [2, 2, 101, 9901]), 
238
            #(4000005, [3, 3, 5, 103, 863]), 
239
            #(4000006, [2, 2000003]), 
240
            #(4000007, [11, 79, 4603]), 
241
            #(4000008, [2, 2, 2, 3, 166667]), 
242
            #(4000009, [13, 307693]), 
243
            #(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
244
            #(1000001, [101, 9901]), (1000002, [2, 3, 166667]), 
245
            #(1000003, [1000003]), 
246
            #(1000004, [2, 2, 53, 53, 89]), 
247
            #(1000005, [3, 5, 163, 409]), 
248
            #(1000006, [2, 7, 71429]), 
249
            #(1000007, [29, 34483]), 
250
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
251
            #(1000009, [293, 3413]), 
252
            #(100, [2, 2, 5, 5]), 
253
            #(101, [101]), 
254
            #(103, [103])])
255
256-
#D has 23 entries
256+
257
#integer: 0
258
#D has 23 entries
259
260
#==============OUTPUT AFTER RESTARTING PROGRAM======
261
# Items 100, 101, and 103 are missing!
262
263
#integer: 4
264
#the factors of 4 are [2, 2]
265
#dict_items([(1000000, [2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
266
            #(1000001, [101, 9901]), 
267
            #(1000002, [2, 3, 166667]), 
268
            #(1000003, [1000003]), 
269
            #(1000004, [2, 2, 53, 53, 89]), 
270
            #(1000005, [3, 5, 163, 409]), 
271
            #(1000006, [2, 7, 71429]), 
272
            #(1000007, [29, 34483]), 
273
            #(1000008, [2, 2, 2, 3, 3, 17, 19, 43]), 
274
            #(1000009, [293, 3413]), 
275
            #(4000007, [11, 79, 4603]), 
276
            #(4000001, [41, 97561]), 
277
            #(4000002, [2, 3, 666667]), 
278
            #(4000000, [2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5]), 
279
            #(4000008, [2, 2, 2, 3, 166667]), 
280
            #(4000003, [7, 139, 4111]), 
281
            #(4000009, [13, 307693]), 
282
            #(4000004, [2, 2, 101, 9901]), 
283
            #(4000006, [2, 2000003]), 
284
            #(4, [2, 2]), 
285
            #(4000005, [3, 3, 5, 103, 863])])
286
287
#D has 21 entries
288
#integer: 0
289
#D has 21 entries
290
291
#===================END OF OUTPUTS=======================