944,051 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8835
  • C RSS
Sep 14th, 2004
1

Pythagorean Triples

Expand Post »
I need to make a program for school and I have no idea how to make it. I am supposed to do:
Quote ...
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....
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004
Sep 14th, 2004
1

Re: Pythagorean Triples

You need to loop through...hope this helps.
  1. for ( int side1 = 1; side1 < 500; side1++ ) {
  2.  
  3. for ( int side2 = 1; side2 < 500; side2++ ) {
  4.  
  5. for ( int hypt = 1; hypt < 500; hypt++ )

Post your code so we can see what you have so far...that would help :rolleyes:
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Sep 14th, 2004
0

Re: Pythagorean Triples

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004
Sep 14th, 2004
0

Re: Pythagorean Triples

Error...

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int side1 = 0;
  7. int side2 = 0;
  8. int hypotenuse = 0;
  9.  
  10. for( int side1 = 0; side1 <= 500; side1++ )
  11. {
  12. if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )
  13. {
  14. cout << side1 << " * " << side1 << " + " << side2 << " * " << side2 << " = " << hypotenuse << " * " << hypotenuse << endl;
  15. }
  16.  
  17. for( int side2 = 0; side2 <= 500; side2++ )
  18. {
  19. if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )
  20. {
  21. cout << side1 << " * " << side1 << " + " << side2 << " * " << side2 << " = " << hypotenuse << " * " << hypotenuse << endl;
  22. }
  23.  
  24. for( int hypotenuse = 0; hypotenuse <= 500; hypotenuse++ )
  25. {
  26. if( ( side1 * side1 ) + ( side2 * side2 ) = ( hypotenuse * hypotenuse ) )
  27. {
  28. cout << side1 << " * " << side1 << " + " << side2 << " * " << side2 << " = " << hypotenuse << " * " << hypotenuse << endl;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. system( "pause" );
  35. return 0;
  36. }
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...
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004
Sep 14th, 2004
1

Re: Pythagorean Triples

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Sep 14th, 2004
0

Re: Pythagorean Triples

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...
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004
Sep 14th, 2004
0

Re: Pythagorean Triples

  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3. int count = 0;
  4. int hyptSquared;
  5. int sidesSquared;
  6. long loopcounter = 0;
  7.  
  8. for ( int side1 = 1; side1 < 500; side1++ ) {
  9.  
  10. for ( int side2 = 1; side2 < 500; side2++ ) {
  11.  
  12. for ( int hypt = 1; hypt < 500; hypt++ ) {
  13. hyptSquared = hypt * hypt;
  14. sidesSquared = side1 * side1 + side2 * side2;
  15. ++loopcounter;
  16.  
  17. if ( hyptSquared == sidesSquared ) {
  18. cout << side1 << "\t" << side2 << "\t"
  19. << hypt << endl;
  20. ++count;
  21. }
  22. }
  23. }
  24. }
  25. cout << "the inner loop looped " << loopcounter << " times." << endl;
  26. cout << "A total of " << count << " triples where found!" << endl;
  27. cin.get();
  28. return 0;
  29. }
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Sep 14th, 2004
1

Re: Pythagorean Triples

Playing along at home, this is what I had.
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. int a, b, c, i = 0, limit = 500;
  6. puts("Pythagorean Triples:");
  7. for ( a = 1; a < limit; ++a )
  8. {
  9. for ( b = a + 1; b < limit; ++b )
  10. {
  11. for ( c = b + 1; c < limit; ++c )
  12. {
  13. if ( a * a + b * b == c * c )
  14. {
  15. printf("%3d : { %3d, %3d, %3d }\n", ++i, a, b, c);
  16. }
  17. }
  18. }
  19. }
  20. return 0;
  21. }
The first several outputs are as follows.
Quote ...
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 }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 15th, 2004
0

Re: Pythagorean Triples

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:
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Copying 2D array by pointers
Next Thread in C Forum Timeline: Using sound card in C?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC