Sir, I am not able to think a way to limit my time please help me

code is :

#include
using namespace std;

int main(){

unsigned long long int n;
cin >> n;
unsigned long long int x = n;
x++;
unsigned long long int y = x;
y++;

while(((n*n) + (x*x)) != y*y){
	
	while(((n*n) + (x*x)) > y*y){
		y++;
	}
	while(((n*n) + (x*x)) < y*y){
		x++;
	}
}
cout << x << " " << y;

}

Hey @prerakvarshney12
First, suppose n is the shortest side of the triangle, m, k are other two sides. According to Pythagorean Theorem, we know n^2 + m ^2 = k ^2

just do a change k^2 - m ^2 = n ^2

futherly ( k + m )( k - m ) = n^2

as we know, n ^2 × 1 = n ^2 so we can suppose that k + m = n ^2, k - m = 1

easily, we get

because the side is a interger, so this solution can only be used when n is a odd.

So how to deal with even? we find that if (k-m) is odd, the solution is suitable for odd. On the other hand, we guess that if(k-m) is even, the solution is suitable for even.

just as this,

so, we get

this is an O (1) algorithm.

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.