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:
What's my mistake?
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;
}
}
}
}