I tinkered with this, just because, and found a couple ways to help speed up and make safer the brute force method. FWIW.
Code:
/**
* Display Pythagorean triples up to 'size'.
* @param size the maximum length of a side
* @return the number of Pythagorean triples found
* @example <code>unsigned int count = pythagorean_triples(500);</code>
* @Notes Being brute force, this gets very slow for large values of size.
* @Notes This code <i>does</i> check for integer overflow with the squared
* values.
*/
unsigned int pythagorean_triples(unsigned int size)
{
unsigned int a, b, c, len = 0, count = 0;
for ( a = 1; a < size && a < UINT_MAX / a; ++a )
{
for ( b = a + 1; b < size && b < UINT_MAX / b; ++b )
{
for ( c = b + 1; c < size && c < UINT_MAX / c; ++c )
{
unsigned int lhs = a * a + b * b;
unsigned int rhs = c * c;
if ( lhs < rhs )
{
break; /* rhs will only get bigger */
}
else if ( lhs == rhs )
{
if ( ++count == UINT_MAX )
{
goto done;
}
len += printf("(%u,%u,%u),", a, b, c);
if ( len >= 60 )
{
len = 0;
putchar('\n');
}
fflush(stdout);
break; /* no reason to continue checking more c's */
}
}
}
}
done:
if ( len )
{
putchar('\n');
}
return count;
}