オイラーは1774年にx^2-x+41がx=1,2,\ldots,40においてすべて素数になることを発表(ベルリン・アカデミーの紀要が出版)
そこで、40個以上の素数を生成する式はないのだろうかと、いざpythonコーディング。
import math
from mpmath import *
from pprint import pprint
mp.pretty = True
from sympy import *
from spb import * # SymPy Plotting Backends (SPB)
import japanize_matplotlib
import matplotlib.pyplot as plt
# import numpy as np
from fractions import Fraction as Frac
init_printing()
#init_printing(order='rev-lex')
#var("a:z")
from sympy.abc import *
#f = Function("f")
# Prime x^2-px+n
def f(x,p,n):
# return x**2 + x + n
return x**2 - p*x + n
for m in range(1,100):
# print('\n p =',prime(m))
for n in range(1000):
max = 0
count = 0
countn = 1
for x in range(100):
if countn == 0 and isprime(f(x,prime(m),n)) == 1 and isprime(f(x+1,prime(m),n)) == 1:
if count > max:max = count
count = 0
countn = 1
if countn == 1 and isprime(f(x,prime(m),n)) == 1 and isprime(f(x+1,prime(m),n)) == 1:
# if countn == 1 and isprime(x**4 + x**3 + x**2 + x + n) == 1 and isprime((x+1)**4 + (x+1)**3 + (x+1)**2 + (x+1) + n) == 1:
# if countn == 1 and isprime(x**3 + x**2 + n) == 1 and isprime((x+1)**3 + (x+1)**2 + n) == 1:
count = count + 1
countn = 1
else:
countn = 0
if max > 40:
# print('n=',n,'#',max)
print('x²-{}x+{} #{} m={}'.format(prime(m),n,max,m))
実行結果
$ python x^2-px+n.py
x²-3x+43 #41 m=2
x²-5x+47 #42 m=3
x²-7x+53 #43 m=4
x²-11x+71 #45 m=5
x²-13x+83 #46 m=6
x²-17x+113 #48 m=7
x²-19x+131 #49 m=8
x²-23x+173 #51 m=9
x²-29x+251 #54 m=10
x²-31x+281 #55 m=11
x²-37x+383 #58 m=12
x²-41x+461 #60 m=13
x²-43x+503 #61 m=14
x²-47x+593 #63 m=15
x²-53x+743 #66 m=16
x²-59x+911 #69 m=17
x²-61x+971 #70 m=18
x²-61x+971が連続する70個の素数を生成することがわかった
そこで、素数を書き出してみると
1番目 f(1)=911
2番目 f(2)=853
3番目 f(3)=797
4番目 f(4)=743
5番目 f(5)=691
6番目 f(6)=641
7番目 f(7)=593
8番目 f(8)=547
9番目 f(9)=503
10番目 f(10)=461
11番目 f(11)=421
12番目 f(12)=383
13番目 f(13)=347
14番目 f(14)=313
15番目 f(15)=281
16番目 f(16)=251
17番目 f(17)=223
18番目 f(18)=197
19番目 f(19)=173
20番目 f(20)=151
21番目 f(21)=131
22番目 f(22)=113
23番目 f(23)=97
24番目 f(24)=83
25番目 f(25)=71
26番目 f(26)=61
27番目 f(27)=53
28番目 f(28)=47
29番目 f(29)=43
30番目 f(30)=41
31番目 f(31)=41
32番目 f(32)=43
33番目 f(33)=47
34番目 f(34)=53
35番目 f(35)=61
36番目 f(36)=71
37番目 f(37)=83
38番目 f(38)=97
39番目 f(39)=113
40番目 f(40)=131
41番目 f(41)=151
42番目 f(42)=173
43番目 f(43)=197
44番目 f(44)=223
45番目 f(45)=251
46番目 f(46)=281
47番目 f(47)=313
48番目 f(48)=347
49番目 f(49)=383
50番目 f(50)=421
51番目 f(51)=461
52番目 f(52)=503
53番目 f(53)=547
54番目 f(54)=593
55番目 f(55)=641
56番目 f(56)=691
57番目 f(57)=743
58番目 f(58)=797
59番目 f(59)=853
60番目 f(60)=911
61番目 f(61)=971
62番目 f(62)=1033
63番目 f(63)=1097
64番目 f(64)=1163
65番目 f(65)=1231
66番目 f(66)=1301
67番目 f(67)=1373
68番目 f(68)=1447
69番目 f(69)=1523
70番目 f(70)=1601

