def find_well_distr(s): def find_distrib(s): if s == "": return [dict()] l = [] for dictio in find_distrib(s[1:]): if s[0] in dictio: l.append(dictio) else: for i in range(10): if i not in dictio.values(): d = dictio.copy() d[s[0]] = i l.append(d) return l ll = find_distrib(s) l_well = [] for dictio in ll: if dictio[s[0]] == 0: continue l = [str(dictio[lettre]) for lettre in s] nb = int("".join(l)) if is_square(nb): l_well.append(dictio) return l_well
def c_b(k, b0=0): b = b0 + 1 while not is_square(4 * k + 5 * b**2): b += 1 if b >= (k / 5)**0.5: return None return b, int((4 * k + 5 * b**2)**0.5)
def problem_44(): def pent(n): return (n * (3 * n - 1)) // 2 a = 1 while True: d = pent(a) a += 1 dd = 2 * d i = 1 ok = False while i * (3 * (i + 2) - 1) <= dd: if dd % i == 0: j = dd // i if j % 3 != 2: i += 1 continue i2 = (j + 1) // 3 if i2 < i: i += 1 continue if (i + i2) % 2 != 0: i += 1 continue k = (i + i2) // 2 s = 3 * k**2 - k - d delta = 1 + 24 * s if is_square(delta) and int(delta**0.5 + 1) % 3 == 0: ok = True print(d) break i += 1 if ok: break
def problem_66(): def a_b(D): h1, k1 = 1, 0 a_i = int(D ** 0.5) r = 1, 1, -a_i, 1 h2, k2 = a_i, 1 while h2 ** 2 - D * k2 ** 2 != 1: a, b, c, d = r a2, b2 = a * b * d ** 2, a ** 2 * d ** 2 * D - c ** 2 * b ** 2 c2, d2 = -c * b ** 2 * d, a ** 2 * d ** 2 * D - c ** 2 * b ** 2 a, b, c, d = a2, b2, c2, d2 d1 = gcd(abs(a), abs(b)) a //= d1 b //= d1 a_i = int(a * (D ** 0.5) / b + c / d) c -= a_i * d d2 = gcd(abs(c), abs(d)) c //= d2 d //= d2 r = a, b, c, d h1, h2 = h2, a_i * h2 + h1 k1, k2 = k2, a_i * k2 + k1 return h2, k2 x_max = 9 D_max = 5 for D in range(10, 1001): if not is_square(D): x = a_b(D)[0] if x > x_max: D_max = D x_max = x print(D_max)
def problem_64(): def period(D): h1, k1 = 1, 0 a_i = int(D ** 0.5) r = 1, 1, -a_i, 1 h2, k2 = a_i, 1 L = [] while r not in L: L.append(r) a, b, c, d = r a2, b2 = a * b * d ** 2, a ** 2 * d ** 2 * D - c ** 2 * b ** 2 c2, d2 = -c * b ** 2 * d, a ** 2 * d ** 2 * D - c ** 2 * b ** 2 a, b, c, d = a2, b2, c2, d2 d1 = gcd(abs(a), abs(b)) a //= d1 b //= d1 a_i = int(a * (D ** 0.5) / b + c / d) c -= a_i * d d2 = gcd(abs(c), abs(d)) c //= d2 d //= d2 r = a, b, c, d h1, h2 = h2, a_i * h2 + h1 k1, k2 = k2, a_i * k2 + k1 return len(L) - L.index(r) c = 0 for N in range(2, 10001): if not is_square(N) and period(N) % 2 == 1: c += 1 print(c)
def f(n): nb = 0 for x in range(n // 2 + 1, int((n/2)*(1+2**0.5))+1): c_2 = n ** 2 - 4 * x ** 2 + 4 * n * x if is_square(c_2): nb += 1 if nb > 105: return 500 return nb * 4
def next1(b): from Tools import is_square while not is_square(1 + 5 * b**2): b += 1 return int(sqrt(1 + 5 * b**2) + 0.05), b
def next_x(n, x=0): x += 1 while not is_square(2 * n**2 - (2 * x - n)**2) and x <= n: x += 1 return x if x <= n else 0
def nb(a, b, c, d): s = 0 s += dic[(a, b)] s += dic[(b, c)] s += dic[(c, d)] s += dic[(d, a)] s += a + b + c + d - 3 return s ll = [] for aa in range(1, m + 1): for bb in range(aa + 1, m + 1): for cc in range(aa + 1, m + 1): for dd in range(bb, m + 1): if is_square(nb(aa, bb, cc, dd)): ll.append((aa, bb, cc, dd)) n_tot = 0 for aa, bb, cc, dd in ll: if bb == dd: n_tot += 1 # print(aa, bb, cc, dd, "ll", 1) else: n_tot += 2 # print(aa, bb, cc, dd, "ll", 2) n_tot *= 4 ll2 = [] for aa in range(1, m + 1): bb = aa
# %% Problem 141 from Tools import is_square s = set() for b in range(1, 10**6): for p in range(1, 10**6): if p**2 * b >= 10**6: break a = p + 1 c = a**3 * b**2 * p + p**2 * b while c <= 10**12: if is_square(c): s.add(c) a += 1 c = a**3 * b**2 * p + p**2 * b print(sum(s)) # sol = 878454337159 # Todo: Too long ?
if abs(nb_ - nn) < mm: mini = m, n mm = abs(nb_ - nn) print(mini[0] * mini[1]) # sol = 2772 # %% Problem 86 from math import sqrt from Tools import is_square c = 0 n = 0 while n < 10**6: c += 1 for d in range(c + 1, int(c * 5**0.5 + 0.1) + 1): if is_square(d**2 - c**2): m = int((d**2 - c**2)**0.5 + 0.1) if m <= c + 1: n += m // 2 else: n += (2 * c - m) // 2 + 1 # %% Problem 87 from Tools import numpy_sieve l = [False for i in range(50000000)] l_pp = list(numpy_sieve(7070)) for i in l_pp: for j in l_pp: