-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path058_spiral_primes.py
62 lines (48 loc) · 1.33 KB
/
058_spiral_primes.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
"""
idea:
"""
import time
import math
import primesieve
def main():
n = 100000
primes = set(primesieve.primes(1000000))
it = primesieve.Iterator()
# i=2 n=i*4=8 sum=9 diagonal points= sum+(sum-i)+(sum-2*i)+(sum-3*i)
total = 1
num_of_primes = 0
diagonal_nums = 1
for i in range(2, n, 2):
total += i * 4
diagonal_nums += 4
it.skipto(total-3*i - 1)
num_of_primes += 1 if it.next_prime() == total-3*i else 0
it.skipto(total-2*i - 1)
num_of_primes += 1 if it.next_prime() == total-2*i else 0
it.skipto(total-i - 1)
num_of_primes += 1 if it.next_prime() == total-i else 0
it.skipto(total - 1)
num_of_primes += 1 if it.next_prime() == total else 0
ratio = num_of_primes / diagonal_nums
print('i', i+1, 'ratio', ratio)
if ratio < 0.1:
print("i", i+1, 'total', total)
break
def is_prime(n):
if n % 2 == 0:
return False
a = math.ceil(math.sqrt(n))
b = a**2 - n
while not math.sqrt(b).is_integer():
b += 2*a + 1
a += 1
y = math.sqrt(b)
a = a + y
if int(a) == 1 or int(n/a) == 1:
return True
else:
return False
if __name__ == '__main__':
start_time = time.time()
main()
print("time:", time.time() - start_time)