pythagorean triples exercise
jgaynor at ncsa.uiuc.edu
Thu Oct 21 17:03:10 CEST 2010
What you want is to realize that all integer Pythagorean triples can be generated by a pair of integers, (i,j), j < i. The values are just (* = multiply, ^ = exponentiation)
a = 2*i*j
b = i^2 - j^2
c = i^2 + j^2 (hypotenuse)
So yes indeed a^2 + b^2 = c^2. This is a very ancient result, btw and used to be taught in public schools until recently.
So the programming problem is to sort through these and toss out all triangles for which the short side (a or b, it will vary) is less than or equal to n. Or you could be Math-y and use an inequality argument to find when a or b is the short side (bit of work).
Hope this helps...
----- Original Message -----
From: "Baba" <raoulbia at gmail.com>
To: python-list at python.org
Sent: Thursday, October 21, 2010 5:51:07 AM
Subject: pythagorean triples exercise
i need a hint regarding the following exercise question:
"Write a program that generates all Pythagorean triples whose small
sides are no larger than n.
Try it with n <= 200."
what is "n" ? i am guessing that it is a way to give a bound to the
triples to be returned but i can't figure out where to fit in "n".
a^a + b^b = c^c is the condition to satisfy and i need to use loops
and "n" will be an upper limit of one (or more?) of the loops but i am
a bit lost. Please help me get thinking about this right.
exercise source: Java by Dissection (Ira Pohl and Charlie McDowell)
More information about the Python-list