本次来解决欧拉计划的第816题:
解:
第一步:最原始的算法
先从简单的情况开始,即原题里的14个点的情况
import math
def gen_points(n):
s = [0] * (2*n)
s[0] = 290797
for i in range(1, 2*n):
s[i] = (s[i - 1] * s[i - 1]) % 50515093
p = [(s[2 * i], s[2 * i + 1]) for i in range(n)]
return p
def euclidean_distance(p1, p2):
return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
# 求最小距离,同时返回两点的序号
def find_min_dist(p):
min_dist = float('inf')
for i in range(len(p)):
for j in range(i + 1, len(p)):
distance = euclidean_distance(p[i], p[j])
if distance < min_dist:
min_dist = distance
mi = i
mj =