def main(limit): mem = [0] * (limit + 1) perims = map(sum, pythagoreanTriples()) perims = takewhile(lambda p: p <= limit, perims) for p in perims: mem[p] += 1 return max(range(limit + 1), key=lambda i: mem[i])
def main(): xs = defaultdict(set) for p, q, x in pythagoreanTriples(): for r, s in xs[x]: for y, z in ((p, r), (r, p), (p, s), (s, p), (q, r), (r, q), (q, s), (s, q)): if y > z and square(y * y - z * z): if all(square(n) for n in [x+y,x-y,x+z,x-z,y+z,y-z]): return x + y + z xs[x].add((p, q))
def main(limit): # unwrap the box and draw straight lines from S to F # we see that the path is a hypotenuse # to get integer paths use pythagorean triples mem = [0] * 4 for p, q, r in pythagoreanTriples(): # we let p, q = a, b + c or q, p = a, b + c for maxSide, solns in countSolns(p, q): if maxSide >= len(mem): # double the length of the list mem.extend([mem[-1]] * len(mem)) for m in range(maxSide, len(mem)): # count this M toward larger Ms mem[m] += solns # p + q + r = p + q + sqrt(p * p + q * q) < 3*M + sqrt(5) * M M = int((p + q + r) / (3 + sqrt(5))) # Ms smaller or equal to this are fixed, ie no new solutions possible if mem[M] > limit: # Once we find a fixed M over the limit we should see if there is one smaller # I have a hunch that the one we found is the smallest but I can't prove it M = 1 + next(m for m in reversed(range(M)) if mem[m] <= limit) # smallest M that passes the limit return M
def main(limit): # unwrap the box and draw straight lines from S to F # we see that the path is a hypotenuse # to get integer paths use pythagorean triples mem = [0] * 4 for p, q, r in pythagoreanTriples( ): # we let p, q = a, b + c or q, p = a, b + c for maxSide, solns in countSolns(p, q): if maxSide >= len(mem): # double the length of the list mem.extend([mem[-1]] * len(mem)) for m in range(maxSide, len(mem)): # count this M toward larger Ms mem[m] += solns # p + q + r = p + q + sqrt(p * p + q * q) < 3*M + sqrt(5) * M M = int( (p + q + r) / (3 + sqrt(5)) ) # Ms smaller or equal to this are fixed, ie no new solutions possible if mem[M] > limit: # Once we find a fixed M over the limit we should see if there is one smaller # I have a hunch that the one we found is the smallest but I can't prove it M = 1 + next( m for m in reversed(range(M)) if mem[m] <= limit) # smallest M that passes the limit return M
def main(lim): # trick: generate the triples in order of increasing perimeter, check adjacent perimeters for differences perimeters = map(sum, pythagoreanTriples()) perimeters = takewhile(lambda L: L <= lim, perimeters) return sum(sum(1 for _ in g) == 1 for _, g in groupby(perimeters))
def countPaths(M): maxPerim = (3 + sqrt(5)) * M triples = takewhile(lambda t: sum(t) <= maxPerim, pythagoreanTriples()) return sum(countTriples(p, q, M) for p, q, _ in triples)
def main(n): return next(a * b * c for a, b, c in pythagoreanTriples() if a + b + c == n)