Pythagoras Triplet

Only 2 test cases of Pythagoras triplet are passing.
I have tried using both biginteger and ong datatypes. For long its showing wrong answer and for biginteger its showing time limit exceeded

Hey @S19MLPP0010,
You don’t need a very elaborate solution for this problem.

You can use the following approach:

  1. If n<0, n=1, n=2. Print -1 and return because squares aren’t possible for these numbers.
  2. Now if n is even. We will simplify the Pythagorean equation.

a^2 + b^2 = c^2. Hence we manipulate the equation as a^2 = (c+b)(c-b). Now if the given number is even i.e. a is even, we can express it as product of 2 even numbers say 2 and (a^2)/2.

(a^2)/2 * 2 = (c-b)(c+b)
Hence c+b = 2 and c-b = (a^2)/2. Now solve for b and c and do similarly if given n is odd. (NOTE: I have taken a = n)

Also, when the complexity and calculations are huge in any problem. There’s usually a simple equation behind that, you can also try to deduce that in questions of such type.

And, for all future doubts kindly attach an IDE link of your code as well. https://ide.codingblocks.com You can also use the coding blocks IDE.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.