Project Euler #39: Integer right triangles

  • + 0 comments

    Python 3 Code Happy hacking

    import math

    def gcd(a, b): while b != 0: a, b = b, a % b return a

    primiter = [0] * (5000 * 1000 + 1) for m in range(1, 2001): for n in range(1, m): if (m + n) % 2 and gcd(m, n) == 1: p = 2 * m * m + 2 * m * n if p > 5000000: continue primiter[p] += 1 i = 2 while i * p <= 5000000: primiter[i * p] += 1 i += 1

    ans = [0] * (5000 * 1000 + 1) ans[12] = 12 for i in range(13, 5000 * 1000 + 1): if primiter[i] > primiter[ans[i - 1]]: ans[i] = i else: ans[i] = ans[i - 1]

    t = int(input()) while t > 0: n = int(input()) print(ans[n]) t -= 1