Pythagorean Triples

Reply

Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Pythagorean Triples

 
1
  #1
Sep 14th, 2004
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....
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: Pythagorean Triples

 
1
  #2
Sep 14th, 2004
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:
big146
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Re: Pythagorean Triples

 
0
  #3
Sep 14th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Re: Pythagorean Triples

 
0
  #4
Sep 14th, 2004
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Pythagorean Triples

 
1
  #5
Sep 14th, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Re: Pythagorean Triples

 
0
  #6
Sep 14th, 2004
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...
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: Pythagorean Triples

 
0
  #7
Sep 14th, 2004
  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. }
big146
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,341
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pythagorean Triples

 
1
  #8
Sep 14th, 2004
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.
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 }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Re: Pythagorean Triples

 
0
  #9
Sep 15th, 2004
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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC