def __init__(self, p=Fraction(0), q=Fraction(0), ext=2): self.p = p self.q = q self.ext = ext if is_square(ext): self.p += lsqrt(ext) self.q = 0 while is_square(self.ext): self.ext = lsqrt(self.ext)
def find_min_solution(D): y = 1 if(is_square(D)): return None while(True): xsq_candidate = D*(y**2) + 1 if(is_square(xsq_candidate)): return (lsqrt(xsq_candidate), y) y += 1
def solve(L): solutions = {} for a in range(1, L/4 + 1): if a % 100 == 0: print(a) for b in range(a+1, (L-2*a)/2 + 1): absq = a**2 + b**2 if is_square(absq): c = lsqrt(absq) try: solutions[a + b + c].append((a,b,c)) except KeyError, e: solutions[a + b + c] = [(a,b,c)]
def solve(N): result = 0 #@UnusedVariable perimeter_to_sides = {} for a in range(1, N + 1): for b in range(a, N-a + 1): if is_square(a**2 + b**2): p = perimeter(a, b) if p > N: break c = lsqrt(a**2+b**2) try: perimeter_to_sides[p].append((a,b,c)) except KeyError, e: perimeter_to_sides[p] = [(a,b,c)]
def solve(n): for a in range(0, int(n/2) + 1): asq = a**2 start = max(a+1, n/2 - a) end = n/2 for b in range(start, end + 1): bsq = b**2 csq_candidate = asq + bsq if(is_square(csq_candidate) and csq_candidate > bsq): c = lsqrt(csq_candidate) if((a + b + c) == n): return (a, b, c) return None
# Like #137, use generating function # sum(G_{n+2}*z^n) = sum(G_{n+1}*z^n) + sum(G_n*z^n) Recurrence # g(z) = sum(G_n*z^n), => g(z)/z^2 - G_2/z^2 - G1/z = g(z)/z - G1/z + g(z) # => g(z) = (1+3z)/(1-z-z^2), A = z*g(z) = (z+3z^2)/(1-z-z^2) Discriminant: 5A^2 + 14A + 1 from number import is_square # Alpern Diophantine solver # Starting pairs (0, +-1) (-3, +-2) (-4, +-5) (2, +-7) xy = [(0, -1), (0, 1), (-3, -2), (-3, 2), (-4, -5), (-4, 5)] # (2, +-7) unneeded nuggets = set() for i in range(15): print([p[0] for p in xy]) for pair in xy: x = pair[0] if x > 0 and is_square(5*x**2 + 14*x + 1): nuggets.add(x) for j in range(len(xy)): x = xy[j][0] y = xy[j][1] xy[j] = (-9*x - 4*y - 14, -20*x - 9*y - 28) print(sorted(nuggets)) print(sum(sorted(nuggets)[:30]))
# G = 1 + z + 2z^2 + ... = 1/(1 -z - z^2) # A(x) = x*G = x/(1 - x - x^2) Discriminant = 5A^2 + 2A + 1 # Recurrence relations from http://www.alpertron.com.ar/QUAD.HTM from number import is_square def r(x, y): return (-9*x - 4*y - 2, -20*x - 9*y - 4) x1, y1 = 0, 1 x2, y2 = 0, -1 x3, y3 = -1, -2 x4, y4 = -1, 2 nuggets = set() for i in range(20): print(x1, x2, x3) for x in (x1, x2, x3): # x3 and x4 generate same solutions if x > 0 and is_square(5*x**2 + 2*x + 1): nuggets.add(x) x1, y1 = r(x1, y1) x2, y2 = r(x2, y2) x3, y3 = r(x3, y3) x4, y4 = r(x4, y4) print(sorted(nuggets)) print(sorted(nuggets)[14])