pythagorean triples exercise

Tim Roberts timr at probo.com
Sun Oct 24 05:55:52 CEST 2010


Baba <raoulbia at gmail.com> wrote:
>
>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".

Yes.  That's saying your program should read a value for N, check that N is
not larger then 200, then generate the Pythagorean triples where A and B
are less than N.

>a^a + b^b = c^c is the condition to satisfy

No, it's not.  It's a^2 + b^2 = c^2, where a, b, and c are integers.
Perhaps you meant a*a + b*b = c*c.

>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.

The simplest (but not most efficient) method is brute force, using three
loops, one each for a, b, and c.  You can compute the largest "c" you will
need by computing the square root of a*a+b*b.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list