1

I want to write a code in C language to output a list of triples matching the Pythagorean triple in a range of positive integers (a2 = b2 + c2 with a, b and c ∈ ℤ+) using only nested while () loops.

I wrote this:

//Programme to output possible Pythagorean triples in a range of unsigned integers.

#include <stdio.h>

int main(void) {

    unsigned int i = 1, j = 1, ij = 1;

    puts("\n***To calculate Pythag. triple integers...***\n");
    while (i <= 250) {
        while (j <= 250) {
            while (ij <= 250) {
                if (ij * ij == i * i + j * j) {
                    printf ("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
                ++ij;
            }
            ++j;
        }
        ++i;
    }
    return 0;
}

It does not work. It only shows the text in the first puts() function.

output:

enter image description here

What's my mistake?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Vynylyn
  • 172
  • 11

2 Answers2

4

After ending a loop with i=1,j=1,ij=251, you never enter the innermost loop again because you never set ij back to 1. Same for j.

Fixed:

    unsigned i = 1;
    while (i <= 250){
        unsigned j = 1;
        while (j <= 250){
            unsigned ij = 1;
            while (ij <= 250){
                if (ij*ij == i*i + j*j) {
                    printf("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
                ++ij;
            }
            ++j;
        }
        ++i;
    }

We usually write this as follows:

    for ( unsigned i = 1; i <= 250; ++i ) {
        for ( unsigned j = 1; j <= 250; ++j ) {
            for ( unsigned ij = 1; ij <= 250; ++ij ) {
                if (ij*ij == i*i + j*j) {
                    printf("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
            }
        }
    }

This is still a while loop, but using the for keyword/syntax.


Note that it might be faster to precalculate the 250 squares. We can also do the additions fewer times. And we can break out of the inner loop once we find a match.

    unsigned squares[251]; // 0..250
    for ( unsigned i = 1; i <= 250; ++i ) {
        squares[i] = i*i;
    }

    for ( unsigned i = 1; i <= 250; ++i ) {
        unsigned ii = squares[i];
        for ( unsigned j = 1; j <= 250; ++j ) {
            unsigned jj = squares[j];
            unsigned iipjj = ii+jj;
            for ( unsigned k = 1; k <= 250; ++k ) {
                unsigned kk = squares[k];
                if (kk == iipjj) {
                    printf("Candidate triple: (%u, %u, %u)\n", k, i, j);
                    break;
                }
            }
        }
    }
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Well, I don't know much about for-loops and that thing [] which might be a list or array. So I will stick to that while() for now till I reach those topics. – Vynylyn Jul 27 '22 at 06:44
2

After inner loop exit it's variable isn't reinitialized and remain 251. You should initialize variable inside of outer loop:

   while (i <= 250){
        j = 1;
        while (j <= 250){
            ij = 1;
            while (ij <= 250){
                if (ij*ij == i*i + j*j){
                    printf ("Candidate triple: (%u, %u, %u)\n", ij, i, j);
                }
                ++ij;
            }
            ++j;
        }
        ++i;
    }
dimich
  • 1,305
  • 1
  • 5
  • 7