I wrote this code to get a number from user and if it is possible to make a right angle triangle with it I want it to give out the numbers from smallest to biggest one it works properly but I want to know if there is a more efficient way(giving the result faster) to code such thing.
it's working slowly it's a long process to find the answer I want to make it faster
#include <iostream>
bool isTriangle( int n, int& i, int& j, int& k )
{
for ( i=1; i<n; ++i ) {
int ii = i*i; // cache value
for ( j=1; j<i; ++j ) {
for ( k=1; k<=j; ++k ) {
if ( k*k+j*j==ii and i+j+k==n ) {
returntrue;
}
}
}
}
returnfalse;
}
int main()
{
int n=0, a, b, c;
// input can fail or have insane value:
if ( std::cin >> n and 0 < n and isTriangle( n, a, b, c ) )
std::cout << c << " " << b << " " << a;
else
std::cout << "Impossible";
return 0;
}