using a triple-nested for...next loop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have to write a program to find all Pythagorean triples for a right
triangle. I know that the squares of two sides of the triangle must equal
the square of the third (longest) side. However, I am not sure how to use a
triple-nested for...next loop to try all possibilities.

For instance, take a look at these numbers:

a 3 5 7 8 9 11
b 4 12 24 15 40 60
c 5 13 25 17 41 61

Notice that in each case "a" squared plus "b" squared is equal to the sum of
c squared. My program has to compute such a table by iterating through
several combinations of numbers to locate ones that meet this criteria.

Any suggestions would be helpful.
 
Is this a homework/school assignment?

Anyways, there are an infinite number of Pythagorean triples, so what
other information can you provide us? i.e. Are you given a value for
one of the sides? Any of the angles of the triangle (other than the 90
degree)? Or any other useful information?

Thanks,

Seth Rowe
 
This is for a class. I have to write the program to find all Pythagorean
triples for side1, side2 and hypotenuse, none larger than 30. And I have been
instructed to use a triple-nested For...Next loop that tries all
possibilities.
 
I suggest a strategy. Do not know if it's the best.

.Make a long list L (hashtable) of squared numbers.
For each choice of one squared number q in L ( 2,4,9, .. )

iterate all the squared numbers p less than sqrt(q),
and any time r = (q - p) is in the list L add the triple (q, p, r).

Let me know if makes sense...


-P

Candace ha scritto:
 
Well then, since this is for a class, I won't help you cheat yourself
out of a learning experience. Struggling through a problem now will
help you in the future, as you will learn good problem solving skills
(that don't include quering newsgroups) that apply to more than just
programming. Just try to simplify the problem and take it one step at a
time...

Thanks,

Seth Rowe
 
I was not looking for the answers, just direction. Thanks to those of you who
responded to this message with direction.
 
Back
Top