-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_gaps.py
More file actions
40 lines (34 loc) · 1 KB
/
prime_gaps.py
File metadata and controls
40 lines (34 loc) · 1 KB
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
# will calculate P_n+1 - P_n
import sys
LIMIT = 10000
# higher the LIMIT, better the accuracy
def main():
primes = [] # it'll store primes
for i in range(1, LIMIT): # cheking for primes
if isPrime(i):
primes.append(i) # adding to our array
difference = [] # it'll store the differences
for i in range(len(primes) - 1):
diff = primes[i+1] - primes[i] # P_n+1 - P_n
difference.append(diff)
print("Max difference:", max(difference))
def isPrime(n):
"""
checks for a factor of n between 2 and (n - 1),
if a factor is found, return False,
if not return True.
"""
if n <= 0:
# returning error
sys.stderr.write("Better luck next time\n")
sys.exit(1)
if n == 1:
return False # 1 is'nt prime. is it?
if n == 2:
return True
for i in range(2, n-1):
if n % i == 0: # checking for factors
return False
return True # if prime return true
if __name__ == "__main__":
main()