Post #154
645

краткий апдейт на тему t.me/compmathweekly/141
- 🔥 42
- 👍 7
- ❤ 3
- 👏 2
КО @compmathweekly
import numpy as np
A = np.array([[4,1],[2,1]])
for _ in range(5):
A = A / A.sum(axis=1, keepdims=True)
A = A / A.sum(axis=0, keepdims=True)
print(A)






def simulated_annealing(N, max_iter, T=0.2, cooling=0.9975):
centers = np.random.uniform(0, 1, (N, 2))
best_centers = centers.copy()
best_R = R = max_radius(centers)
history = [R]
for step in range(max_iter):
i = step%N
old_pos = centers[i].copy()
old_dist = point_dist(i, centers)
step_size = min(T**0.5,0.04)
centers[i] += np.random.normal(0, step_size, 2)
centers[i] = np.clip(centers[i], 0.0, 1.0)
delta = point_dist(i, centers) - old_dist
if delta > 0 or np.random.random() < np.exp(delta / T):
R = max_radius(centers)
if R > best_R:
best_R = R
best_centers = centers.copy()
else:
centers[i] = old_pos
T *= cooling
history.append(R)
return best_centers, best_R, history
from fractions import Fraction
import math
N = 20
x, y = x0, y0 = 3, 5
u = [0]*(N+1)
u[1] = x0.denominator
for n in range(2,N+1):
k = Fraction(y-y0, x-x0) if (x,y) != (x0,y0) \
else Fraction(3*x0*x0, 2*y0)
x = k*k-x0-x
y = -(k*(x-x0)+y0)
b = math.isqrt(b2 := x.denominator)
assert b**2 == b2
u[n] = b
v = [0]*(N+1)
v[:5] = [0, 1, 10, 171, -7660]
c1, c2 = v[2]**2, -v[3],
for n in range(5,N+1):
b = c1*v[n-1]*v[n-3]+c2*v[n-2]**2
assert b%v[n-4] == 0
v[n] = b//v[n-4]
print(f"n = {n:2d}: {u[n] == v[n] or u[n] == -v[n]} ({len(str(u[n])):3d} digits)")
from fractions import Fraction
# y^2 = x^3 - 2
def add(P, Q):
x1, y1 = P
x2, y2 = Q
k = Fraction(y2 - y1, x2 - x1) if P!=Q \
else Fraction(3*x1*x1, 2*y1)
x3 = k*k - x1 - x2 # Vieta
y3 = -(k*(x3 - x1) + y1)
return (x3, y3)
P = (3, 5)
Q = P
for n in range(2,14):
Q = add(Q, P)
print(f"[{n:2d}] {Q[0]}")

import math
import matplotlib.pyplot as plt
x, y, phi = 0, 0, 0
def move(s=1,color='blue'):
global x, y
x0, y0 = x, y
x = x0 + s*math.cos(phi)
y = y0 + s*math.sin(phi)
plt.plot([x0,x],[y0,y],color=color)
def turn(s):
global phi
phi += s*2*math.pi
for k in range(100):
move()
turn(1/100)
plt.gca().set_aspect('equal')
plt.show()
p = 101
for k in range(p):
move()
turn((2*k+1)/p)


import matplotlib.pyplot as plt
from math import log
ns = range(3_000)
xs = [pow(3,n) for n in ns]
ans = [x.bit_count() for x in xs]
c = log(3)/(2*log(2))
appr = [n*c for n in ns]
plt.plot(ns,ans)
plt.plot(ns,appr)
plt.title(r'2-digit sums of $3^n$')
plt.tight_layout()
plt.show()