I need to make a program for school and I have no idea how to make it. I am supposed to do:

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 Pythagorean triples for side1, side2 and 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 sheer brute force.

I know how to make the triple nested loops but I get lost when I try to figure out how to make it loop each number to 500 as different numbers and not all the same number. Example of all I can do: (1 * 1) + (1 * 1) = (1 * 1) and with 2s and with 3s but of course the only number that would work is 1....

You need to loop through...hope this helps.

for ( int side1 = 1; side1 < 500; side1++ ) {

		for ( int side2 = 1; side2 < 500; side2++ ) {

			for ( int hypt = 1; hypt < 500; hypt++ )

Post your code so we can see what you have so far...that would help :rolleyes:

What the hell? That is what I had but I got an error. I'm going to go try again.

Wait a second, that part I know is correct. I think I have an idea now. I'm going to go try it out.

Error...

#include <iostream>
  using namespace std;
  
  int main()
  {
  	int side1 = 0;
  	int side2 = 0;
  	int hypotenuse = 0;
  
  	for( int side1 = 0; side1 <= 500; side1++ )
  	{
  		if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )
  		{
 			cout << side1 << " * " << side1 << " + " << side2 << " * " << side2 << " = " << hypotenuse << " * " << hypotenuse << endl;
  		}		
  		
  		for( int side2 = 0; side2 <= 500; side2++ )
  		{
 			if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )
  			{
 		 	cout << side1 << " * " << side1 << " + " << side2 << " * " << side2 << " = " << hypotenuse << " * " << hypotenuse << endl;
  			}	
  			
 			for( int hypotenuse = 0; hypotenuse <= 500; hypotenuse++ )
  			{
 		 	if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )
  				{
 		 		cout << side1 << " * " << side1 << " + " << side2 << " * " << side2 << " = " << hypotenuse << " * " << hypotenuse << endl;
  				}
  			}
  		}
  	}
  
  	system( "pause" );
  	return 0;
 }

Error C2106 : '=' : left operand must be l-value. That error is on every line with an if in it. Actually the whole thing is wrong because I don't think that would even solve my problem...

Greetings,

I did see a few errors that evidently caught my attention.

Firstly, on line 6 of your posted code you have the following:

int side1 = 0;

Though 4 lines later, [or line 10], you state the following:

for( int side1 = 0; side1 <= 500; side1++ )

What this is doing is creating an integer called side1, which may cause errors since you already defined an integer of the same name on line 6. The same goes for side2 and hypotenuse. Since the variable already exists, the for loop shouldn't contain the int data type.

Also, in your calculation of checking if the two sides equal the hypotenuse will always pass since asking if a variable equal or equals is different. See why:

if (a = b) { b = a; } // will not ensure a equals b (why, because you are setting a to b not checking for comparing)
if (a == b) { b = a; } // this ensures a equals b using ==

Likewise with your following statement if this makes sense:

if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )

Other than that, I haven't tried to compile your code, and do not know where the C2106 error lies within.


I hope this helps,
- Stack Overflow

DUH. I want so smack myself in the head for restating the integers. But the error was because I didn't put a == because it is a question and not an assignment. DUH........

I'm going to keep trying again but I don't think I'll get it. I already missed the due date but I don't think anyone else got it either so o well...

int _tmain(int argc, _TCHAR* argv[])
{
	int count = 0;
	int hyptSquared;
	int sidesSquared;
	long loopcounter = 0;

	for ( int side1 = 1; side1 < 500; side1++ ) {

		for ( int side2 = 1; side2 < 500; side2++ ) {

			for ( int hypt = 1; hypt < 500; hypt++ ) {
				hyptSquared = hypt * hypt;
				sidesSquared = side1 * side1 + side2 * side2;
				++loopcounter;

				if ( hyptSquared == sidesSquared ) {
					cout << side1 << "\t" << side2 << "\t"
						<< hypt << endl;
					++count;
				}
			}
		}
	}
	cout << "the inner loop looped " << loopcounter << " times." << endl;
	cout << "A total of " << count << " triples where found!" << endl;
	cin.get();
	return 0;
}

Playing along at home, this is what I had.

#include <stdio.h>
 
 int main(void)
 {
    int a, b, c, i = 0, limit = 500;
    puts("Pythagorean Triples:");
    for ( a = 1; a < limit; ++a )
    {
 	  for ( b = a + 1; b < limit; ++b )
 	  {
 		 for ( c = b + 1; c < limit; ++c )
 		 {
 			if ( a * a + b * b == c * c )
 			{
 			   printf("%3d : { %3d, %3d, %3d }\n", ++i, a, b, c);
 			}
 		 }
 	  }
    }
    return 0;
 }

The first several outputs are as follows.

Pythagorean Triples:
1 : { 3, 4, 5 }
2 : { 5, 12, 13 }
3 : { 6, 8, 10 }
4 : { 7, 24, 25 }
5 : { 8, 15, 17 }
6 : { 9, 12, 15 }
7 : { 9, 40, 41 }
8 : { 10, 24, 26 }
9 : { 11, 60, 61 }
10 : { 12, 16, 20 }

I get it now. I goes 1-500 in the third loop for every 1 in the second loop. Then the third 1-500 for every 1 in the second loop 500 times for every 1 in the first loop. I dunno if I explained that the way I am thinking it but thanks I get it now. :mrgreen:

This article has been dead for over six months. Start a new discussion instead.