We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #39: Integer right triangles
You are viewing a single comment's thread. Return to all 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