Hey guys I'm working on a Pythagorean Triple program.
In my program everything worked and I found all the possible Pythagorean triples below the value 500. Unfortunately I had repeats due to the fact that the legs of the triangle could just "swap" value. I tried to correct the problem by using a three dimensional array called "used." Because I have a maximum of 500 on each side of the triangle I had to use 500 three times in my boolean value "used." I think this is just too large a value for my compiler but maybe I did something wrong. Check it out.
Code:void calculate() { int side1 = 1; int side2 = 1; int hypo = 1; int triplecounter = 0; 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; used[side2][side1][hypo] = true; cout << "A Pythagorean triple is " << hypo << " " << side1 << " " << side2 << "\n\n"; triplecounter = (triplecounter + 1); } } } } } } cout << triplecounter << "\n\n"; }



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.