Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ (OPTIONAL. Lastname Lab4_p6.cpp.) (Pythagorean Triples) A right triangle can

ID: 3669719 • Letter: C

Question

c++

(OPTIONAL. Lastname Lab4_p6.cpp.) (Pythagorean Triples) A right triangle can have sides that are all integers. The 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 square of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2 and the hypotenuse all no larger than 500. Use a triple-nested for-loop that tries all possibilities. (This is an example of "brute force" computing. You will learn in more advanced computer science courses that there are many interesting problems for which there is no known algorithmic approach other than using sheer brute force.)

Explanation / Answer

#include <iostream>
#include <time.h>
using namespace std;

int main() {
   clock_t start, end;
   start = clock(); //Start clock

   int count = 0; //Number of triples found
   long int hypotenuseSquared; //Hypotenuse squared
   long int sidesSquared; //Sum of squares of sides

   //Column Titles
   cout << "Side 1 Side 2 Side3" << endl;

   //side1 values range from 1 to 500
   for (long int side1 = 1; side1 <= 500; side1++) {
       //side2 values range from current side1 to 500
       for (long int side2 = side1; side2 <= 500; side2++) {
           //Hypotenuse values range from current side2 to 500
           for (long int hypotenuse = side2; hypotenuse <= 500; hypotenuse++) {
               //Calculate square of hypotenuse value
               hypotenuseSquared = hypotenuse * hypotenuse;

               //Calculate sum of squares of sides
               sidesSquared = (side1 * side1) + (side2 * side2);

               //If (hypotenuse)^2 = (side1)^2 + (side2)^2, Pythagorean triple
               if (hypotenuseSquared == sidesSquared) {
                   //Display triple
                   cout << side1 << ' ' << side2 << ' ' << hypotenuse << ' ';
                   count++; //Update count
               }
           }
       }
   }

   //Display total number of triples found
   cout << "A total of " << count << " triples were found." << endl;

   end = clock(); //End clock

   cout << "Time required for execution: " << (double)(end-start)/CLOCKS_PER_SEC << " seconds.";
}

output

Side 1 Side 2 Side3                                                                                                                                       
3       4       5                                                                                                                                           
5       12      13                                                                                                                                          
6       8       10                                                                                                                                          
7       24      25                                                                                                                                          
8       15      17                                                                                                                                          
9       12      15                                                                                                                                          
9       40      41                                                                                                                                          
10      24      26                                                                                                                                          
11      60      61                                                                                                                                          
12      16      20                                                                                                                                          
12      35      37                                                                                                                                          
13      84      85                                                                                                                                          
14      48      50                                                                                                                                          
15      20      25                                                                                                                                          
15      36      39                                                                                                                                          
15      36      39                                                                                                                                          
15      112     113                                                                                                                                         
16      30      34                                                                                                                                          
16      63      65                                                                                                                                          
17      144     145                                                                                                                                         
18      24      30                                                                                                                                          
18      80      82                                                                                                                                          
19      180     181                                                                                                                                         
20      21      29                                                                                                                                          
20      48      52                                                                                                                                          
20      99      101                                                                                                                                         
21      28      35                                                                                                                                          
21      72      75                                                                                                                                          
21      220     221                                                                                                                                         
22      120     122                                                                                                                                         
23      264     265                                                                                                                                         
282     376     470                                                                                                                                         
285     380     475                                                                                                                                         
288     330     438                                                                                                                                         
288     384     480                                                                                                                                         
291     388     485                                                                                                                                         
294     392     490                                                                                                                                         
297     304     425                                                                                                                                         
297     396     495                                                                                                                                         
300     315     435                                                                                                                                         
300     400     500                                                                                                                                         
319     360     481                                                                                                                                         
320     336     464                                                                                                                                         
325     360     485                                                                                                                                         
340     357     493                                                                                                                                         
A total of 386 triples were found.                                                                                                                          
Time required for execution: 0.067651 seconds.sh-4.3$                                                                                                       

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote