I'm not really going to post code or ask for code but rather ask for anyones thought on how the pseudocode should go for this problem: Pythagorean Triples: A right triangle can have sides that are all integers. A set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all _unique_ Pythagorean triples for side1, side2 and hypotenuse, all no longer than 500. How many are there? Note that this problem specifically asks you to exclude the duplicates. For example, if you were to check all unique Pythagorean Triples less than 5 (rather than 500) your output must be 3 4 5 That is, it must NOT output both (3,4,5) and (4,3,5). my thought: im pretty sure there needs to be multiple for (or while, whatever) loops for this but im not sure how to go about this problem. i figure the outter loop would have to incriment from 1 to 500 and incorporate the Pythagorean theorem. can anyone give me some insight, again, not asking for code, jsut ideas.

Just skimming this, the most obvious way to me (i.e. not optimized) would be to use three nested for loops.

ok i wrote it up real quick and it looks pretty good except i keep getting a "expected primary-expression before ')' token " which occurs during the if statement, can anyone help me out?

#include <iostream> #include <math.h> using namespace std; int main() { int side1, side2, hypotenuse, counterloops, countertrip; for ( side1=0; side1 < 500; side1++) { for ( side2=0; side2 < 500; side2++) { for ( hypotenuse=0; hypotenuse < 500; hypotenuse++) { counterloops++; if ( ((side1 * side1) + (side2 * side2)) == (hypotenuse * hypotenuse))) { cout << side1 << "\t" << side2 << "\t" << hypotenuse"\n"; countertrip++; } } } cout << " The innermost for loops is entered " << counterloops << " times"; cout << " There are " << countertrip << " Pythagorean triples"; return 0; } }

This : if ( ((side1 * side1) + (side2 * side2)) == (hypotenuse * hypotenuse))) { should be this: if ( ((side1 * side1) + (side2 * side2)) == (hypotenuse * hypotenuse)) {

Use two nested integer loops: one for Side 1 and one for Side 2. Increment each by 1, to a max of 500. Inside both loops, perform the Pythagorean formula to calculate the hypotenuse. If the hypotenuse is an integer, print out the lengths of all three sides.

That's why you static cast the hypotenuse length, instead of rounding it. ( if (hyp == static_cast<int>(hyp)) {/*do something*/} ... ) You're going to have to truncate it eventually anyway, whether to check if it's an integer or to check if it's equal to another variable that happens to be an integer. If you use three loops, the program will take 500x longer to run, and the prof will probably penalize him for being so inefficient.

If you go to wikipedia, there is actually a formula for all the triples (Thank you Euler read:Euclid). Its the solution to the system of equations given. http://en.wikipedia.org/wiki/Pythagorean_triple

How does static_cast solve the problem of the inherent inaccuracy of floating point numbers? e.g. There is no guarantee that 8.9999999999 or 9.0000000004 is actually 9. Doing it with 3 loops without using floating point guarantees accuracy.