1

Here is the question

Find all Pythagorean Triples for side1, side2 and hypotenuse all no longer than 500. Use a triple nested for loops that tries possibilities.

The below is my attempt

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int side1 = 0;
    int side2 = 0;
    int rightSide = 0;

    cout << "Right Side" << setw(10) << "Side1" << setw(10) << "Side2" << endl;

    for(int i=1;i<=500;i++)
    {
        side1++;
        //cout << side1 << endl;

        for(int a=1;a<=500;a++)
        {
            side2++;
            //cout << "side 2 " << side2 << endl;

            for(int c=1;c<=500;c++)
            {
                rightSide++;
                int rightSideSqr = rightSide*rightSide;
                int side1Sqr = side1*side1;
                int side2Sqr = side2*side2;

                if(rightSideSqr == side1Sqr+side2Sqr)
                {
                    cout << rightSideSqr << setw(15) << side1 << setw(10) << side2 << endl;
                 }


            }
        }
    }
}

But it gives no success, seems like an infinite loop. Please help.

Please Note: I am new to C++, I study it by my self. And, this is not an homework, I made the problem statement because it is the best way to express the issue.

EDIT

Right Side Side1 Side2

RUN SUCCESSFUL (total time: 1s)

EDIT 2

Working code

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //int side1 = 0;
    //int side2 = 0;
    //int rightSide = 0;

    cout << "Right Side" << setw(10) << "Side1" << setw(10) << "Side2" << endl;

    for(int i=1;i<=500;i++)
    {
        //side1++;
        //cout << side1 << endl;

        for(int a=1;a<=500;a++)
        {
            //side2++;
            //cout << "side 2 " << side2 << endl;

            for(int c=1;c<=500;c++)
            {
                //rightSide++;
                int rightSideSqr = c*c;
                int side1Sqr = i*i;
                int side2Sqr = a*a;

                if(rightSideSqr == (side1Sqr+side2Sqr))
                {
                    cout << rightSideSqr << setw(15) << i << setw(10) << a << endl;
                 }


            }
        }
    }
}
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 1
    It's not inf. loop. The loop will take a few hours (?) to complete, though. You only need 2 loops for the 2 sides, and use sqrt to get the hypotenuse; or hypotenuse + 1 side and sqrt to get the other side . The program should output results under 1 second. – nhahtdh Sep 19 '12 at 17:41
  • Hi, Thanks for the reply. I edited it. If I am not printing that side2, the out put is NOTHING! Please have a look at the edit – PeakGen Sep 19 '12 at 17:46

2 Answers2

3

It's not an infinite loop, it's just a very slow finite loop. I/O is slow -- you're printing out 500*500 = 250,000 lines of text in the cout statement in the middle loop, and printing out 250,000 lines of text to the console is very, very slow. If you remove that print statement, it will execute much more quickly.

Secondly, you have an error in your logic. The variables side1, side2, and rightSide never get reset to 0 at the appropriate times, so they just keep incrementing beyond their intended values. Try resetting them to 0, or just use the loop counters instead of extra variables like that.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
1

Besides from the problems pointed out by @Adam Rosenfield. The program won't finish fast with 3 loops like that, if the limit is raised a bit higher.

First observation is that you don't need the 3rd inner loop, since the hypotenuse can be calculated and compared. Calculate the hypotenuse from the 2 sides, make it into integer, and check whether the Pythagorean equality still holds.

You can make some more observations: the 2 sides are interchangeable, so we only need to loop up to sqrt(5002/2) (going over will just reverse the sides). For the 2nd inner loop for the other side of the right triangle, since we know the upper limit, we can cut down the number of loops by looping up to sqrt(5002 - side12).

Pseudocode, not C code (^ in C code is XOR, but I use ^ in the pseudocode below to denote power). round() will round to the nearest integer (you might need to implement this). The upper limit for the 2 loops can be cached just before entering the loop, since their value won't change during the loop:

for (side1 = 1; side1 <= round(sqrt(500^2 / 2)); side1++) {
    for (side2 = side1; side2 <= round(sqrt(500^2 - i^2)); side2++) {
        hypo = round(sqrt(side1^2 + side2^2))

        if (hypo^2 == side1^2 + side2^2) {
           printResult
        }
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • Thanks for the reply, I really appreciate it. I believe you gave me something to think again :) – PeakGen Sep 19 '12 at 18:04