-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherdos_p6.py
More file actions
64 lines (52 loc) · 1.81 KB
/
erdos_p6.py
File metadata and controls
64 lines (52 loc) · 1.81 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
See description here: https://www.erdosproblems.com/6
Let d_{n} = p_{n+1} − p_{n}
Are there infinitely many n such that d_{n} < d_{n+1} < d_{n+2} ?
increasing percentage ~= 0.148609 * number of primes
decreasing percentage ~= 0.147695 * number of primes
"""
from primesieve import primes
import matplotlib.pyplot as plt
from math import log
def main():
n = 10000000
ls_p = primes(n)
ls_d = [p2-p1 for p1, p2 in zip(ls_p, ls_p[1:])]
# print(ls_p)
# print(ls_d)
# plt.plot(ls_p, label='prime')
# plt.plot(ls_d, label='difference')
# plt.show()
increasing = 0
decreasing = 0
k = 0
ls_percent_increase = []
ls_percent_decrease = []
for d1, d2, d3 in zip(ls_d, ls_d[1:], ls_d[2:]):
k += 1
if d1 < d2 < d3:
print(f'{k=}, increasing {d1=}, {d2=}, {d3=}')
increasing += 1
if d1 > d2 > d3:
print(f'{k=}, decreasing {d1=}, {d2=}, {d3=}')
decreasing += 1
ls_percent_increase.append(increasing/k)
ls_percent_decrease.append(decreasing/k)
print(f'{n=} has {increasing=}, {decreasing=}')
plt.plot(ls_percent_increase, label='increasing', color='g')
plt.plot(ls_percent_decrease, label='decreasing', color='r')
plt.title('Sequence of differences of primes')
plt.xlabel('Prime number')
plt.ylabel('Percentage')
plt.gca().set_yticklabels(['{:.0f}%'.format(val * 100) for val in plt.gca().get_yticks()])
plt.legend(loc='lower right')
plt.show()
ls_diff_increase = [x1 - x2 for x2, x1 in zip(ls_percent_increase[1:], ls_percent_increase)]
plt.plot(ls_diff_increase)
plt.yscale('log')
plt.xlabel('Prime number')
plt.ylabel('Log Scale Difference')
plt.title('Difference of increasing sequence')
plt.show()
if __name__ == '__main__':
main()