| | |
Pythagorean Triples
![]() |
•
•
Join Date: Apr 2004
Posts: 14
Reputation:
Solved Threads: 0
I need to make a program for school and I have no idea how to make it. I am supposed to do: 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....
•
•
•
•
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.
You need to loop through...hope this helps.
Post your code so we can see what you have so far...that would help :rolleyes:
C Syntax (Toggle Plain Text)
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:
big146
•
•
Join Date: Apr 2004
Posts: 14
Reputation:
Solved Threads: 0
Error...
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...
C Syntax (Toggle Plain Text)
#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; }
Greetings,
I did see a few errors that evidently caught my attention.
Firstly, on line 6 of your posted code you have the following:
Though 4 lines later, [or line 10], you state the following:
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:
Likewise with your following statement if this makes sense:
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
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Apr 2004
Posts: 14
Reputation:
Solved Threads: 0
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...
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...
C Syntax (Toggle Plain Text)
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; }
big146
Playing along at home, this is what I had. The first several outputs are as follows.
C Syntax (Toggle Plain Text)
#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; }
•
•
•
•
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
![]() |
Similar Threads
- Finding pythagorean triples using triple-nested for loop (C++)
- Code Snippet: Pythagorean Triples (Java)
- Code Snippet: Pythagorean Triples (C++)
- pythagorean triple (C++)
Other Threads in the C Forum
- Previous Thread: Copying 2D array by pointers
- Next Thread: Using sound card in C?
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






