Skip to content

Instantly share code, notes, and snippets.

@zelinskiy
Created June 5, 2019 13:20
Show Gist options
  • Save zelinskiy/495a518cf25efbfd63012bc7ad5a8347 to your computer and use it in GitHub Desktop.
Save zelinskiy/495a518cf25efbfd63012bc7ad5a8347 to your computer and use it in GitHub Desktop.
import turtle
import math
# даны 2 числа, берем 2 ближайшие к ним точки на ковре улама, считаем кратчайший путь
def prime(n):
if n == 2:
return True
if n % 2 == 0 or n <= 1:
return False
sqr = int(math.sqrt(n)) + 1
for divisor in range(3, sqr, 2):
if n % divisor == 0:
return False
return True
def dist(x, y):
return math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
def find_nearest(x, ps):
min_dist = 10**100
min_p = None
for p in ps:
d = dist(x, p)
if d < min_dist and p != x:
min_p = p
min_dist = d
return min_p
# draw triangle intersecting points (a, 0) and (0, b)
a, b = 50, 50
turtle.setup(a * 2, b * 2)
t = turtle.Turtle()
t.penup()
t.speed(0)
def tprint(s):
t.write(s, align="center", font=("Arial", 12, "normal"))
def tlogpos(s):
cells[s] = (int(t.xcor()), int(t.ycor()))
alpha = 1
beta = 2
a = 0
b = 0
start = 12
end = 99
tprint("O")
cells = {}
for i in range(150):
a += 1
if i == start :
tprint("s")
tlogpos("s")
elif i == end :
tprint("e")
tlogpos("e")
elif prime(i):
tprint("⬛")
tlogpos(str(i))
t.forward(40)
if a == alpha:
a = 0
b = (b + 1) % beta
alpha += (b - 1) % beta
t.left(90)
x1 = find_nearest(cells["s"], cells.values())
x2 = find_nearest(cells["e"], cells.values())
t.goto(x1)
t.pencolor("red")
t.pendown()
t.goto(x2)
t.penup()
tprint("ulam ditance = " + str(int(dist(x1, x2))))
#input("Press Enter to exit...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment