-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_3.py
More file actions
49 lines (42 loc) · 884 Bytes
/
Problem_3.py
File metadata and controls
49 lines (42 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os.path
NUM = 600851475143
FileName = 'listOfPrimes.txt'
# Program that prints primes smaller than MAX
# MAX = int(NUM**0.5)
MAX = 2000000
primes = []
if os.path.isfile(FileName):
with open(FileName, 'r') as f:
for line in f:
primes.append(int(line))
else:
with open(FileName, 'w') as f:
f.write('2\n3')
primes.append(2)
primes.append(3)
with open(FileName, 'a') as f:
MAXPRIME = primes[-1]
n = MAXPRIME
while n <= MAX:
for prime in primes:
if n%prime == 0:
break
else:
if n > MAXPRIME:
primes.append(n)
f.write(str(n) + '\n')
if NUM%n == 0:
MAX = NUM/n
print('MAX: %d\n' % MAX)
print(n)
n += 2
lpf = 0
for prime in primes:
if NUM%prime == 0:
lpf = prime
print(prime)
with open(FileName,'w') as f:
for prime in primes:
f.write(str(prime)+'\n')
print(prime)
print('The largest prime factor is: ' + str(lpf))