The test of the Pythagorean theorem does not work?

So, I'm trying to write a program that goes through all possible combination of side lengths for three sides of a triangle and prints those that follow the Pythagorean theorem (i.e. sideA (sqr) + sideB (sqr) = hypotenuse (sqr)). Those that match must print. However, mathematics does not do as it should. The problem, I think, is how I set up my loops.

Then my question is: how do I configure this? Since I got the impression that for loops it runs like this:

The outermost loop checks if the pythagorean condition is satisfied. If so, it again prints the result, increments, and tests. When the condition is not fulfilled, it proceeds to the second cycle, in which the same process is repeated, and at this moment the second cycle goes to the third, the most internal for the cycle. There it will increase to 500, test and print each applicable result, and as soon as this is done, it will go back to the middle cycle. The middle cycle increases, the tests are repeated again, and as soon as the condition is not checked, the code goes into the innermost cycle, and the process repeats, only this time the middle cycle variable changes (i.e., new sets of numbers are tested.) The whole process repeats until until the cycle of the 2nd cycle increases to 500, and at that moment it moves to the outer cycle,and the process will be repeated. The outermost loop increases until the condition is no longer satisfied, then it is transferred to the middle loop, and the two inner loops will go through their procedure again only this time with the changed outer loop variable (i.e., check the new features again).

Any help on this would be appreciated. I would like to move on, but I feel that this concept of a nested loop is very important for understanding, and I do not want to sell myself on this.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {

    int sideA = 1;
    int sideB = 1;
    int hypotenuse = 1;

    for (hypotenuse = 1; hypotenuse <= 500; hypotenuse++) {
        if ( (hypotenuse * hypotenuse) == ( sideA * sideA + sideB * sideB) ) {
            printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
        }

        else {
            for (sideA = 1; sideA <= 500; hypotenuse++) { 
                if ( (hypotenuse * hypotenuse) == ( sideA * sideA + sideB * sideB) ) {
                    printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
                }

                else {
                    for (sideB = 1; sideB <= 500; sideB++) {
                        if ( (hypotenuse * hypotenuse) == ( sideA * sideA + sideB * sideB) ) {
                            printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
                        }      
                    }   
                }
            }           
        }        
    }
}
+4
source share
1 answer

Set to for (sideA = 1; sideA <= 500; hypotenuse++).

Besides the excessive number of ifs, you do not need any of them.

#include <stdio.h>

int main(void)
{
    int sideA, sideB, hypotenuse;

    for (hypotenuse = 1; hypotenuse <= 500; hypotenuse++)
        for (sideA = 1; sideA <= 500; sideA++)
            for (sideB = 1; sideB <= 500; sideB++)
                if ((hypotenuse * hypotenuse) == (sideA * sideA + sideB * sideB))
                    printf("%d, %d, %d\n", sideA, sideB, hypotenuse);
}

http://ideone.com/P01jxB

Also, note that some actions will occur in your code:

  • After the first cycle, sideAand sideB, used by the first if, will no longer be 1, they may not even be connected (501).
  • If the logic falls on any of these ifs, you will not run tests for other values.

, , 125 (500 * 500 * 500). -, , lib, , , math.h:

#include <stdio.h>
#include <math.h>

int main(void)
{
    int a, b;
    for (a = 1; a <= 500; a++)
        for (b = a; b <= 500; b++) // b starts from a, not 1
        {
            double h = sqrt(a * a + b * b);

            if (h > 500.0) // hypotenus is bigger than 500, stop the loop
                break;

            if (fmod(h, 1) == 0.0) // only print if hypotenuse is an integer value
            {
                printf("%d, %d, %d\n", a, b, (int)h);
                printf("%d, %d, %d\n", b, a, (int)h); // print both ways around, optional
            }
        }
    return 0;
}

http://ideone.com/DZFa3s

( ) , 98 .

+3

All Articles