// For: https://forum.arduino.cc/t/does-arduino-have-limitations-with-how-large-the-terminal-number-can-be-in-a-for-loop/1030344/
//
// This is my own implementation.
// I don't check the resulting squareroot, I check the square of c.
//
void setup()
{
Serial.begin(115200);
Serial.println( "a b c");
}
void loop()
{
for( uint32_t a = 1; a<65535; a++)
{
for( uint32_t b = 1; b<65535; b++)
{
// temporary calculation
uint32_t t = (a*a) + (b*b); // bug ! this will not fit
// get the integer squareroot
uint32_t c = sqrt32( t);
// check if it was a good number
if( (c*c) == t)
{
Serial.print( a);
Serial.print( ", ");
Serial.print( b);
Serial.print( ", ");
Serial.print( c);
Serial.println();
}
}
}
}
// Integer squareroot, Newton methode.
// https://en.wikipedia.org/wiki/Integer_square_root
//
// Square root of integer
uint32_t sqrt32( uint32_t s)
{
uint32_t x0 = s / 2; // Initial estimate
// Avoid overflow when s is the maximum representable value
// Sanity check
if( x0 != 0)
{
uint32_t x1 = (x0 + s / x0) / 2; // Update
while(x1 < x0) // This also checks for cycle
{
x0 = x1;
x1 = (x0 + s / x0) / 2;
}
return x0;
}
else
{
return s;
}
}