Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the answer print out twice?

Tags:

c++

I made a program that returns the product abc where a,b,c are pythagorean triples and add up to 1000. The program does output the correct answer but does it twice. I was curious as to why this is so. After playing around with it a bit I found out that it prints out when a = 200 b = 375 c = 425. And once again when a = 375 b = 200 c = 425.

bool isPythagTriple(int a, int b, int c);

int main()
{

    for(int a = 1; a < 1000; a++)
    {
        for(int b = 1; b < 1000; b++)
        {
            for(int c = 1; c < 1000; c++)
            {
                if( ((a+b+c)==1000) && isPythagTriple(a,b,c) )
                {
                    cout << a*b*c << " ";
                    break;
                }
            }
        }
    }

    return 0;
}

bool isPythagTriple(int a, int b, int c)
{
    if( (a*a)+(b*b)-(c*c) == 0 )
        return true;
    else
        return false;
}
like image 365
rEgonicS Avatar asked Mar 06 '26 19:03

rEgonicS


1 Answers

Just for what it's worth, I'd write this function:

bool isPythagTriple(int a, int b, int c)
{
    if( (a*a)+(b*b)-(c*c) == 0 )
        return true;
    else
        return false;
}

More like this:

bool isPythagTriple(int a, int b, int c) { 
    return a*a+b*b==c*c;
}
like image 50
Jerry Coffin Avatar answered Mar 08 '26 07:03

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!