### Pythagorean Triples program

So I have to write the classic "Pythagorean Triples" program that prints out all sets of pythagorean triples where no part of the triple is over 500 (that is, a, b, and c must all be <= 500).

My program is obviously not working (that's why I am here). Take a look and let me know what you see that might be wrong:

 ``123456789101112131415161718192021222324252627282930`` `````` #include #include using namespace std; int main () { int a=1, b=1, c; int i, j, k; double d, e; for (i=1; i<=500; i++) { a += 1; for (j=1; j<=500; j++) { b += 1; for (k=1; k<=500; k++) { e = ((a*a) + (b*b)); d = sqrt(e); c = int(d); if ((c == d) && (c <=500)) cout << "a = " << a << "\t\t b = " << b << "\t\t c = " << c << endl; } } } return 0; } ``````
Why do you need this loop
`for (k=1; k<=500; k++)`
It only prints
 a = (some number) b = (some number) c = (some number)

500 times same line.
For example:
a = 1 b = 1 c = 1.4
a = 1 b = 1 c = 1.4
a = 1 b = 1 c = 1.4
a = 1 b = 1 c = 1.4
....
Well, in truth it do not print, because if ((c == d) && (c <=500)) do not let to print, but it executes this if 500 times with same numbers.
Last edited on
a. A primitive Pythagorean triple (a,b,c) is one in which a, b and c are pairwise coprime. For instance, (3,4,5) is a primitive Pythagorean triple; every multiple of of this (6,8,10), (9,12,15), (12,16,20) ... etc. is a Pythagorean triple.

b. All primitive Pythagorean triples can be generated starting from the triple (3, 4, 5)
http://en.wikipedia.org/wiki/Pythagorean_triple#Parent.2Fchild_relationships

The tree is infinite, but we can stop when c exceeds 500.

`c == d` This will give you problems because you are comparing an int to a double. When the sqrt function was excecuted it introduced small floating point errors that would make d ever so slightly different from the real value. This means that it won't actually equal C. If you want to check for a remainder try something like:
`if ( d%1 < 0.00001 && c <= 500 ) `

Spoiler Alert: I just wrote a version of this that uses Euclid's formula if you're interested. It uses stl containers and algorithms so I'm guessing your teacher won't accept it, but it was fun to make.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include // std::vector #include // std::sort #include // std::ostream, std::cout, std::endl // Structure to hold triples struct triple { int a; int b; int c; bool operator< (triple& t) { return c < t.c; } // Operator function used in std::sort() }; // Function to print triples std::ostream& operator << (std::ostream& o, triple& t) { return o << "a: " << t.a << "\tb: " << t.b << "\tc: " << t.c << std::endl; } // Helper function to check if two integers are coprime bool isCoprime(int m, int n) { for (int i = 2; i < n/2; ++i) if (m%i==0 && n%i==0) // if they can be divided evenly by the same number, they are not coprime return false; return true; } void getPythagoreanTriples(int max, std::vector& set) { for (int m = 2; m <= max; ++m) { for (int n = 1; n < m; ++n) // n cannot be bigger than m { if ( isCoprime(m,n) && ((m-n)%2==1)) // if m and n are coprime and the diff is odd, we have a triple { triple temp; temp.a = m*m - n*n; // using Euclid's formula to calculate a,b,c temp.b = 2*m*n; temp.c = m*m + n*n; if (temp.c > max) return; // When C is larger than 500, return set.push_back(temp); // otherwise add the triple to our set. } } } } int main() { // Make a container to hold all triples std::vector PythagoreanTriples; // Fill the container with all triples getPythagoreanTriples(500, PythagoreanTriples); // Sort the triples std::sort(PythagoreanTriples.begin(), PythagoreanTriples.end()); // Print the triples for (std::vector::iterator it = PythagoreanTriples.begin(); it != PythagoreanTriples.end(); ++it) std::cout << *it; return 0; }`````` ```a: 3 b: 4 c: 5 a: 5 b: 12 c: 13 a: 15 b: 8 c: 17 a: 7 b: 24 c: 25 a: 21 b: 20 c: 29 a: 35 b: 12 c: 37 a: 9 b: 40 c: 41 a: 45 b: 28 c: 53 a: 11 b: 60 c: 61 a: 33 b: 56 c: 65 a: 63 b: 16 c: 65 a: 55 b: 48 c: 73 a: 13 b: 84 c: 85 a: 77 b: 36 c: 85 a: 39 b: 80 c: 89 a: 65 b: 72 c: 97 a: 99 b: 20 c: 101 a: 91 b: 60 c: 109 a: 15 b: 112 c: 113 a: 117 b: 44 c: 125 a: 105 b: 88 c: 137 a: 17 b: 144 c: 145 a: 143 b: 24 c: 145 a: 51 b: 140 c: 149 a: 85 b: 132 c: 157 a: 119 b: 120 c: 169 a: 165 b: 52 c: 173 a: 19 b: 180 c: 181 a: 57 b: 176 c: 185 a: 153 b: 104 c: 185 a: 95 b: 168 c: 193 a: 195 b: 28 c: 197 a: 187 b: 84 c: 205 a: 133 b: 156 c: 205 a: 21 b: 220 c: 221 a: 171 b: 140 c: 221 a: 221 b: 60 c: 229 a: 105 b: 208 c: 233 a: 209 b: 120 c: 241 a: 255 b: 32 c: 257 a: 23 b: 264 c: 265 a: 247 b: 96 c: 265 a: 69 b: 260 c: 269 a: 115 b: 252 c: 277 a: 231 b: 160 c: 281 a: 161 b: 240 c: 289 a: 285 b: 68 c: 293 a: 207 b: 224 c: 305 a: 273 b: 136 c: 305 a: 25 b: 312 c: 313 a: 75 b: 308 c: 317 a: 253 b: 204 c: 325 a: 175 b: 288 c: 337 a: 225 b: 272 c: 353 a: 27 b: 364 c: 365 a: 135 b: 352 c: 377 a: 189 b: 340 c: 389 a: 29 b: 420 c: 421 a: 87 b: 416 c: 425 a: 145 b: 408 c: 433 a: 31 b: 480 c: 481 a: 93 b: 476 c: 485 Press any key to continue . . .```
Last edited on
P.S.
do you need "Pythagorean Triples" or "primitive Pythagorean triple"?

 A Pythagorean triple consists of three positive integers a, b, and c, such that a2 + b2 = c2
 A primitive Pythagorean triple (PPT) is one in which a, b and c are pairwise coprime
Last edited on
Topic archived. No new replies allowed.