-
Notifications
You must be signed in to change notification settings - Fork 2
/
prime_list.py
84 lines (74 loc) · 2.8 KB
/
prime_list.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
################################################################################
## IMPORTED MODULES ##
################################################################################
import os
################################################################################
## GLOBAL VARIABLES ##
################################################################################
MAX_NUMBER_TO_CHECK = 1299821
################################################################################
## PRIMES GENERATION FUNCTIONS ##
################################################################################
# Generate primes with eratostenes method.
def genPrimesEratostenes():
real_primes=[]
primes = dict()
for i in range(2,MAX_NUMBER_TO_CHECK):
primes[i]=True
for i in range(2,MAX_NUMBER_TO_CHECK):
if(i%2500==0):
os.system("clear")
print(str(int((i/MAX_NUMBER_TO_CHECK)*100))+"% completado")
for j in range(i+i,MAX_NUMBER_TO_CHECK,i):
primes[j]=False
for i in range(2,MAX_NUMBER_TO_CHECK):
if primes[i]:
real_primes.append(i)
#return real_primes
f = open("prime_list.txt", "w")
for prime in real_primes:
f.write(str(prime) + ",")
f.close()
f = open("prime_list.txt", "r")
prime_line = f.read()
f.close()
f = open("prime_list.txt", "w")
f.write(prime_line[:-2])
f.close()
# Method to generate small primes in a file.
def genPrimes():
primes = []
for i in range(2,MAX_NUMBER_TO_CHECK):
if(i%2500==0):
os.system("clear")
print(str(int((i/MAX_NUMBER_TO_CHECK)*100))+"% completado")
prime=True
for j in range(2,int(i/2+1)):
if i%j==0:
prime=False
break
if prime:
primes.append(i)
f = open("prime_list.txt", "w")
for i in range(len(primes)-2):
f.write(str(primes[i]) + ",")
f.write(str(primes[-1]))
# Filter primes that are 1 or 7 modulus 8.
def checkModulus(factors):
new_factors=[]
for factor in factors:
if factor%8==1 or factor%8==7:
new_factors.append(factor)
return new_factors
################################################################################
## READING PRIMES FROM TXT ##
################################################################################
# Obtain prime list from txt.
def readPrimes():
f = open("prime_list.txt", "r")
prime_line = f.read()
primes_str = prime_line.split(",")
primes=[]
for prime_str in primes_str:
primes.append(int(prime_str))
return primes