-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProb58_SpiralPrimes.py
84 lines (75 loc) · 1.82 KB
/
Prob58_SpiralPrimes.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
#!/usr/bin/env python
import math
from collections import defaultdict
def factorize(n):
if n < 1:
raise ValueError('fact() argument should be >= 1')
if n == 1:
return [] # special case
res = []
# iterate over all even numbers first.
while n % 2 == 0:
res.append(2)
n //= 2
# try odd numbers up to sqrt(n)
limit = math.sqrt(n+1)
i = 3
while i <= limit:
if n % i == 0:
res.append(i)
n //= i
limit = math.sqrt(n+i)
else:
i += 2
if n != 1:
res.append(n)
return res
def num_divisors(n):
factors = sorted(factorize(n))
histogram = defaultdict(int)
for factor in factors:
histogram[factor] += 1
# number of divisors is equal to product of
# incremented exponents of prime factors
from operator import mul
try:
return reduce(mul, [exponent + 1 for exponent in list(histogram.values())])
except:
return 1
def is_prime(num):
if num % 2 == 0:
return False
if num % 3 == 0 and num != 3:
return False
if num_divisors(num) == 2 and num > 1:
return True
else:
return False
def spiral_diagonals():
n = 1
step = 2
since_last = 0
while True:
yield n
n += step
since_last += 1
if since_last == 4:
step += 2
since_last = 0
def main():
level = 0
primes = 0
for i, n in enumerate(spiral_diagonals()):
if (i-1) % 4 == 0:
level += 1
if is_prime(n):
primes += 1
side_length = (2 * level) + 1
ratio = float(primes) / float(i+1)
if 0 < ratio < 0.1:
print side_length
print primes
print i+1
return
if __name__ == "__main__":
main()