Pythagorean triplet

#include
#include<math.h>
using namespace std;
int main()
{
int n;
cin>>n;
float h,b;
if(n%2==0)
{
h=pow((n/2),2)+1;
b=pow((n/2),2)-1;
if(h==int(h) && b==int(b))
{
cout<<b<<endl;
cout<<h<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
else if(n%2!=0)
{
h=(pow(n,2)+1)/2;
b=(pow(n,2)-1)/2;
if(h==int(h) && b==int(b))
{
cout<<b<<endl;
cout<<h<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
return 0;
}

What is the problem in this code?

is the input provided not the smallest side of the pythagorean triplet??

hey @dsdishu99 answer is -1 only when n < 3 else the answer exist so you dont need to check in other cases.
and since n can be as large as 10^9 n squared can be as large as 10^18 so dont use pow() function just do n * n and use long long and double. Here is code for your reference

#include #include<math.h> using namespace std; int main() { int n; cin>>n; long long int h,b; while(n>3) { if(n%2==0) { h=((n/2)(n/2))+1; b=((n/2)(n/2))-1; if(h==int(h) && b==int(b)) { cout<<b<<endl; cout<<h<<endl; } else { cout<<"-1"<<endl; } } else if(n%2!=0) { h=((nn)+1)/2; b=((nn)-1)/2; if(h==int(h) && b==int(b)) { cout<<b<<endl; cout<<h<<endl; } else { cout<<"-1"<<endl; } } } return 0; }

still not passig all test cases… :frowning:

@dsdishu99 please share your code through, ide.codingbocks.com, it is very difficult to read and work with code in this way.

#include #include<math.h> using namespace std; int main() { int n; cin>>n; long long int h,b; while(n>3) { if(n%2==0) { h=((n/2)(n/2))+1; b=((n/2)(n/2))-1; if(h==int(h) && b==int(b)) { cout<<b<<endl; cout<<h<<endl; } else { cout<<"-1"<<endl; } } else if(n%2!=0) { h=((nn)+1)/2; b=((nn)-1)/2; if(h==int(h) && b==int(b)) { cout<<b<<endl; cout<<h<<endl; } else { cout<<"-1"<<endl; } } } return 0; }

  1. open ide.codingblocks.com
  2. paste your code their
  3. click on save
  4. copy the link and paste it here

@dsdishu99
Use break statements when you print anything
Some sample cases to check :

Q 23
A 264 265

Q 1
A -1

Q 99999979
A 4999997900000220 4999997900000221


please check…not producing output as per second and third instance you suggested…

@dsdishu99
This is the solution

If your doubt is resolved please mark it as closed.

is it not in c language while i am learniing c++…?

please tell me where am i lagging in my solutin?

@dsdishu99
This was C++ only. C++ can also use C based input output and header files style.
Here is your modified solution https://ide.codingblocks.com/s/192415

If your doubt is resolved please mark it as closed.

how would it conclude if it even exists or not…as given in the problem we gotta print -1 when triplet doesn’t exist??

@dsdishu99
For n>3 triplet always exists

so -1 will be returned only if n<3??

@dsdishu99
Yes thats right

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.