So do the same thing but check for lowesti + lowestj == i+j later (once you've found lowesti and lowestj). And if nothing is found then do the first loop again but make sure you don't take the same values for lowesti and lowestj.
Loop a number, n, from a small number, either until you find a match, or until a sufficiently large number.
Change sumSquare to return a bool, and have it keep track of the number of times the pattern you're looking for (A*A + B*B == n) is found. If it's found twice, and (A*A != C*C and A*A != D*D), then return true. Otherwise, if you've exhausted all the choices, return false and try the next highest n.
#include <iostream>
#include <set>
#include <algorithm>
usingnamespace std;
int main()
{
set<int> S;
int d = 0, n = 10000000;
bool best = false;
while( !best ) // Seek smallest n with c <= d and c^2+d^2 seen before
{
best = true;
for ( ++d; best && d * d < n; ++d )
{
int dd = d * d;
int ccmax = min( n - 1 - dd, dd ); // Seek to reduce current n with c <= d
for ( int c = 1; best && c * c <= ccmax; c++ )
{
int test = dd + c * c;
if ( !( S.insert( test ).second ) ) // Seen before
{
n = test;
best = false; // Get out of c and d loops
}
}
}
}
cout << n;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
for ( int n = 2; ; n++ )
{
bool found = false;
int mid = sqrt( n / 2.0 );
for ( int d = mid; d * d < n; ++d )
{
for ( int c = 1; c <= mid ; c++ )
{
if ( c * c + d * d == n )
{
if ( found )
{
cout << n;
return 0;
}
found = true;
}
}
}
}
}