Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 5.20 Pythagorean Triples
- Deitel & Deitel C++ How to Program, 10th ed (Indian subcontinent adaptation)
- Visual Studio Community 2019 */
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main(void) {
- int count{ 0 };
- int a{ 1 }, b{ 1 }, h{ 1 };
- int aSq{ 1 }, bSq{ 1 }, hSq{ 1 };
- // heading
- cout << "side1 (squared) side2 (squared) Sum of squares hypotenuse hypotenuse squared\n\n";
- while (h < 501 && a < 501 && b < 501) {
- for (a = 1; a < 500; a++) {
- for (b = 1; b < 500; b++) {
- for (h = 1; h < 501; h++) {
- aSq = a * a;
- bSq = b * b;
- hSq = h * h;
- if (aSq + bSq == hSq) {
- cout << setw(4) << a << setw(9) << aSq << setw(9) << b
- << setw(9) << bSq << setw(12) << aSq + bSq << setw(12)
- << h << setw(18) << hSq << "\n";
- count++;
- }
- }
- }
- }
- }
- // Values for side1 and side2 eventually get swapped resulting in twice the number of
- // triples. To get unique triples divide total by 2.
- cout << "\nBefore 501 was reached, found " << count/2 << " unique pythagorean triples.\n\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment