You are probably getting a stack overflow because of the size of the "used" array which (assuming 1 byte bools) looks like it weighs in at about 125,000,000 bytes (approx 119 MB... I guess Orbode is thinking 2 byte bools?). That is way too much to allocate on the stack. So you can do a couple things:

1. You could do dynamic memory allocation of the used array:

Code:

bool ***used;
used = new bool**[500];
for( int i = 0; i < 500; ++i )
{
used[i ] = new bool*[500];
for( int j = 0; j < 500; ++j )
{
used[i ][j] = new bool[500];
for( int k = 0; k < 500; ++k ) used[i ][j][k] = false;
}
}

You could allocate a contiguous chunk instead of all the smaller allocations but you are still wasting a lot of space since there are only 386 triples that I found using your code and the above method. Plus you then need to remember to delete all that memory.

2. Create a triple/triangle object (method hinted at by elad) and overload the == operator. Push your triples into a container, checking first to make sure that a match does not already exist in the container. This would only require you to store the 386 objects and that's all. Since you basically already have a "working" program, I'll show you what I was thinking of:

Code:

class triple
{
int side1, side2, hypotenuse;
public:
triple(int s1 = 1, int s2 = 1, int hyp = 1) : side1(s1),
side2(s2),
hypotenuse(hyp)
{
}
bool operator()() const
{
return side1*side1+side2*side2==hypotenuse*hypotenuse;
}
friend bool operator==(const triple& lhs,const triple& rhs);
friend ostream& operator<<(ostream& os,const triple& rhs);
};
bool operator==(const triple& lhs,const triple& rhs)
{
return ( lhs.hypotenuse == rhs.hypotenuse &&
( ((lhs.side1==rhs.side1) && (lhs.side2==rhs.side2)) ||
((lhs.side1==rhs.side2) && (lhs.side2==rhs.side1))
)
);
}
ostream& operator<<(ostream& os,const triple& rhs)
{
return os << hyp << ' ' << side1 << ' ' << side2;
}
...
vector<triple> triples;
for( int i = 1; i <= 500; ++i )
for( int j = 1 j <= 500; ++j )
for( int k = 1; k <= 500; ++k )
{
triple trip(i,j,k);
if( trip() && (find(triples.begin(),triples.end(),trip) == triples.end()) )
triples.push_back(trip);
cout << "A Pythagorean triple is " << trip << endl;
}
cout << triples.size() << endl << endl;

386 triple objects only takes up 4636 bytes (assuming 4 byte ints) then you also have the vectors overhead which is constant regardless of the number of elements it stores so we wind up with a big space savings doing it this way.

BTW, there was one other problem with your code I saw (if you had been able to get it to work as posted):

Code:

bool used[500][500][500] = {false};
for (side1 = 1; side1 <= 500; side1++) {
for (side2 = 1; side2 <= 500; side2++) {
for (hypo = 1; hypo <= 500; hypo++) {
if (used[side1][side2][hypo] == false) {
if (used[side2][side1][hypo] == false) {
if ((hypo * hypo) == (side1 * side1) + (side2 * side2)) {
used[side1][side2][hypo] = true;

Tell me, is *used[500][500][500]* a valid array element? You seemed to forget that arrays are 0-based in C/C++ so the indicies can only go up to 499 and still be valid. I think you meant to put *used[side1-1][side2-1][hypo-1]* instead.