[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Pythagorean Triples

Posted on 1997-03-06
5
Medium Priority
?
591 Views
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.

0
Comment
Question by:fondue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:jos010697
ID: 1162277
Is this a homework problem?
0
 
LVL 2

Accepted Solution

by:
Slarti earned 30 total points
ID: 1162278
#include <iostream.h>
int main()
{
 int a,b,c;
 for(a=1;a<500;a++)
  for(b=a;b<500;b++)
   for(c=b;c<500;c++)
    if(a*a+b*b==c*c)
     cout<<a<<","<<b<<","<<c<<endl;
 return 0;
}
0
 

Expert Comment

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


0
 

Expert Comment

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


0
 
LVL 84

Expert Comment

by:ozo
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;
        cout<<a<<","<<b<<","<<c<<endl;
    }
}
 
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

656 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