You are viewing a single comment's thread. Return to all comments →
I'm not gonna explain a long math magic behind this, but here is much faster version, that can give correct anstwer for every 8-digit n under 7s
` def bound(n): return int(n*(1-math.sqrt(2)/2))
def calc_b(nn, n, a): if nn % (n-a) == 0: return (nn-a*n)//(n-a)
def pit(n): if n % 2 == 1: return -1
nn = n*(n >> 1) # nn = (n^2)/2 for a in range(bound(n), 0, -1): if b := calc_b(nn, n, a): return a*b*(n-a-b) return -1
`
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Project Euler #9: Special Pythagorean triplet
You are viewing a single comment's thread. Return to all comments →
I'm not gonna explain a long math magic behind this, but here is much faster version, that can give correct anstwer for every 8-digit n under 7s
` def bound(n): return int(n*(1-math.sqrt(2)/2))
def calc_b(nn, n, a): if nn % (n-a) == 0: return (nn-a*n)//(n-a)
def pit(n): if n % 2 == 1: return -1
`