pythagorean triples exercise

Alex Hall mehgcap at gmail.com
Thu Oct 21 07:48:57 EDT 2010


On 10/21/10, Baba <raoulbia at gmail.com> wrote:
> Hi everyone
>
> 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".
Picture a right triangle. The two short sides (the two sides coming
off either side of the right angle) must be no longer than n. The
lengths of these two sides are a and b, and you are looping until
either side exceeds n... HTH.
>
> 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.
>
> import math
> for b in range(20):
>     for a in range(1, b):
>         c = math.sqrt( a * a + b * b)
>         if c % 1 == 0:
>             print (a, b, int(c))
>
> this returns
>
> (3, 4, 5) (6, 8, 10) (5, 12, 13) (9, 12, 15) (8, 15, 17) (12, 16, 20)
>
> is that the desired output? what is the step that i'm missing?
>
> thanks in advance
>
> Baba
> p.s. this is not homework but self-study
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap



More information about the Python-list mailing list