Pythagorean Triples

Posted on 1997-03-06
Last Modified: 2008-03-10

2.55      (Pythagorean Triples) A right triangle can have sides that are all integers.  The 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 side l, side2, and the 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 large numbers of interesting problems for which there is no known algorithmic approach other than using sheer brute force.

Question by:fondue

Expert Comment

ID: 1162277
Is this a homework problem?

Accepted Solution

Slarti earned 10 total points
ID: 1162278
#include <iostream.h>
int main()
 int a,b,c;
 return 0;

Expert Comment

ID: 1162279
since 500 * 500 = 250000, you will have to use long ints, otherwise the above code won't work.


Expert Comment

ID: 1162280
since 500 * 500 = 250000, you will have to use long ints, otherwise the above code won't work.

LVL 84

Expert Comment

ID: 1162281
Just so we don't give the impression that there is no known algorithmic approach other than using sheer brute force for this problem:

for( s=1; s<23; s++){
    for( t=s+1; (c = t*t + s*s) < 500; t++ ){
      a = t*t - s*s; b = 2*s*t;

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now