Pythagorean Triplet 54

Back to General discussions forum

Prabhat_SY     2020-12-20 17:47:46
def pytr(n):
    for i in range(1, int(n/3)+1):
        for j in range(i+1, int(n/2)+1):
            k=n-i-j
            if (i*i + j*j == k*k):
                return [i,j,k]
a=int(input())
l=[]
for i in range(a):
    b=int(input())
    c=pytr(b) 
    l.append(c[2]**2)
[print(x,end=' ') for x in l]

Getting answer for small input but for large inputs time limit exceed.

Rodion (admin)     2020-12-28 15:00:08
User avatar

Hi! Your approach is good for small numbers but for larger it could be not quite efficient.

Note it has nested loop about N size, thus about N*N operations - e.g. time consumption grows fast.

So you are expected to find some better, probably somewhat math-related approach.

However feel free to set this problem aside for some time and return to it later. Good luck!

Please login and solve 5 problems to be able to post at forum