Thursday 16 July 2020

16. 5.20 (Pythagorean Triples) A right triangle can have sides whose lengths are all integers. The set of three integer values for the lengths of the sides of a right triangle is called a Pythagorean triple. The lengths of the 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. Write an application that displays a table of the 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 “bruteforce” computing. You’ll learn in more advanced computerscience courses that for many interesting problems there’s no known algorithmic approach other than using sheer brute force.

Q 16. 5.20 (Pythagorean Triples) A right triangle can have sides whose lengths are all integers. The set of three integer values for the lengths of the sides of a right triangle is called a Pythagorean triple. The lengths of the 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. Write an application that displays a table of the 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 “bruteforce” computing. You’ll learn in more advanced computerscience courses that for many interesting problems there’s no known algorithmic approach other than using sheer brute force. 

Code:

#include<iostream>
  using namespace std;
  
  int main()
  {
  int s1 = 0;
  int s2 = 0;
  int hyp = 0;
  
  for( int s1 = 0; s1 <= 500; s1++ )
  {
  if( ( s1 * s1  +  s2 * s2 == hyp * hyp ) )
  {
  cout << s1 << " * " << s1 << " + " << s2 << " * " << s2 << " = " << hyp << " * " << hyp << endl;
  }
 
  for( int s2 = 0; s2 <= 500; s2++ )
  {
  if( ( s1 * s1 ) + ( s2 * s2 ) == ( hyp * hyp ) )
  {
  cout << s1 << " * " << s1 << " + " << s2 << " * " << s2 << " = " << hyp << " * " << hyp << endl;
  }
 
  for( int hyp = 0; hyp <= 500; hyp++ )
  {
  if( ( s1 * s1 ) + ( s2 * s2 ) == ( hyp * hyp ) )
  {
  cout << s1 << " * " << s1 << " + " << s2 << " * " << s2 << " = " << hyp << " * " << hyp << endl;
  }
  }
  }
  }
  
  return 0;

 }

No comments:

Post a Comment