Pythagoras triplet challenge doubt

code https://ide.codingblocks.com/s/46266
question - https://hack.codingblocks.com/contests/c/587/915
please help

Hey Sharad, Pythagoras triplet numbers can be larger so they will not be in range of int. Therefore, you should take long long int instead of int.

1 Like

infact i have changed it , the code is -
#include
#include
using namespace std;
int main()
{
unsigned long long int n,a,b;
cin>>n;
if(n%2==0)
{
a = ((nn)/4)-1;
b = ((n
n)/4)+1;
cout<<a<<" β€œ<<b<<endl;
}
else if(n%2!=0)
{
a = ((nn)-1)/2;
b = ((n
n)+1)/2;
cout<<a<<” β€œ<<b<<endl;
}
else{
cout<<”-1"<<endl;
}
return 0;
}

but still one test case doesn’t work out of 5

https://ide.codingblocks.com/s/46282

Actually your logic is wrong as you are only printing the pair which sum to n . But you should print the pairs (a,b) such that aa + bb =nn (i.e. Pythogoreous triplet) which i think is easy to implement
Just iterate i from 0 to sqrt(n) :
whenver n
n-i*i is integer that means there exits a pythagoean triplet so ,you are supposed to print these pairs

Take a loook on this Pseudo code
https://ide.codingblocks.com/s/46287

1 Like

thanks a lot , but i did some changes with my previous logic and all the testcases were accepted.

1 Like