1000000000以内的满足条件、误差小于1e-07的(a, b)组合:
1: a=71173049, b=82256448, a*pi-b*e=520.0000000000, |error|=0.0000000000
2: a=190751890, b=220456985, a*pi-b*e=520.0000000000, |error|=0.0000000000
3: a=192575648, b=222564752, a*pi-b*e=520.0000000000, |error|=0.0000000000
4: a=194399406, b=224672519, a*pi-b*e=520.0000000000, |error|=0.0000000000
5: a=196223164, b=226780286, a*pi-b*e=520.0000000000, |error|=0.0000000000
6: a=198046922, b=228888053, a*pi-b*e=520.0000000000, |error|=0.0000000000
7: a=310330731, b=358657522, a*pi-b*e=520.0000000000, |error|=0.0000000000
8: a=315802005, b=364980823, a*pi-b*e=520.0000000000, |error|=0.0000000000
9: a=317625763, b=367088590, a*pi-b*e=520.0000000000, |error|=0.0000000000
10: a=323097037, b=373411891, a*pi-b*e=520.0000000000, |error|=0.0000000000
从数学上对代码里的遍历做了优化:
import math
tolerance = 1E-7
i, N = 0, 1_000_000_000
pice, e520 = math.pi / math.e, 520 / math.e
results = []
for a in range(1, N):
apice = a * pice - e520
for b in range(int(round(apice - 1)), int(round(apice + 1)), 1):
value = a * math.pi - b * math.e
error = abs(value - 520)
if error <= tolerance:
i += 1
print(f'{i}:\ta={a:<10d}, b={b:<10d}, a*pi-b*e={value:.010f}, er
r={error:.010f}')
results.append((a, b, error))
# 按误差大小降序输出
results = sorted(results, key=lambda x: x[2])
print(f"{N}以内的满足条件、误差小于{tolerance}的(a, b)组合:")
for i, (a, b, err) in enumerate(results):
print(f'{i+1:>2d}: a={a:<8d}, b={b:<8d}, a*pi-b*e={a*math.pi-b*math.e:.0
10f}, |error|={err:.010f}')
【 在 iMx 的大作中提到: 】
: a=689, b=605, err=-0.0031678943904142822597998255775819932582954300490
: a=1344, b=1362, err=0.0006760633930817191139759780519944224576264086960
: a=6905, b=7789, err=0.0001111696623166836279987936447473198838605590950
: ...................
--
FROM 119.233.241.*