-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP123_prime_square_reminders.py
53 lines (45 loc) · 1.22 KB
/
P123_prime_square_reminders.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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 4 00:43:13 2020
@author: Manish
"""
from math import sqrt
def sieve_of_erat(N):
"""
Function implements sieve of Eratosthenes (for all numbers uptil N).
Returns array erat_sieve
If erat_sieve[i] is True, then 2*i + 3 is a prime.
"""
lim = int(N/2)
if N % 2 == 0:
lim -= 1
erat_sieve = [True]*lim
prime_list = []
prime_list.append(2)
for i in range(int((sqrt(N)-3)/2)+1): # Only need to run till sqrt(n)
if erat_sieve[i] == True:
j = i + (2*i+3)
while j < lim:
erat_sieve[j] = False
j += (2*i+3)
for i in range(lim):
if erat_sieve[i] == True:
prime_list.append(2*i+3)
return erat_sieve, prime_list
def Q123():
N = 1_000_000
N2 = 10**10
_, plist = sieve_of_erat(N)
for i in range(len(plist)):
n = i+1
p = plist[i]
modulo = p**2
r = (pow(p-1,n,modulo) + pow(p+1,n, modulo))% modulo
if r > N2:
print(n)
break
if __name__ == '__main__':
import time
start_time = time.time()
Q123()
print("Program run time(in s): ", (time.time() - start_time))