pythagorean triples exercise

B

Baba

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

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)

thanks

Baba
 
D

Dennis Lee Bieber

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

a^a + b^b = c^c is the condition to satisfy and i need to use loops

Well, I'd interpret it to mean that

a <= 200
AND
b <= 200

since c is the hypotenuse, which by definition is longer than either of
the sides.

The brute force approach is a nested set of "for" loops, running
1-200 (remember that (x)range(200) runs 0-199 <G>).

The alternative is to study the information at

http://www.math.uic.edu/~fields/puzzle/triples.html

and filtering out entries where the first two components are >200...
Looking at the middle term "2*m*n" would have to be less than 201, and
the first term "n*n-m*m" < 201

In Python, this can all be done in a one-liner...

[(n*n - m*m, 2*m*n, n*n + m*m) for n in xrange(2,101) for m in
xrange(1,n) if 2*m*n < 201 and n*n-m*m < 201]

Converting this to a clean set of nested loops and nice lines of
output is a different matter.

Oh, and DO study the link I gave so you can cite it when you turn in
this intriguing formulation... after all, no need for math.sqrt <G>
 
B

Baba

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".
a^a + b^b = c^c is the condition to satisfy and i need to use loops

        Well, I'd interpret it to mean that

a <= 200
AND
b <= 200

since c is the hypotenuse, which by definition is longer than either of
the sides.

        The brute force approach is a nested set of "for" loops, running
1-200 (remember that (x)range(200) runs 0-199 <G>).

        The alternative is to study the information at

http://www.math.uic.edu/~fields/puzzle/triples.html

and filtering out entries where the first two components are >200...
Looking at the middle term "2*m*n" would have to be less than 201, and
the first term "n*n-m*m" < 201

        In Python, this can all be done in a one-liner...

[(n*n - m*m, 2*m*n, n*n + m*m) for n in xrange(2,101) for m in
xrange(1,n) if 2*m*n < 201 and n*n-m*m < 201]

        Converting this to a clean set of nested loops and nice lines of
output is a different matter.

        Oh, and DO study the link I gave so you can cite it when you turn in
this intriguing formulation... after all, no need for math.sqrt <G>

Hi Wulfraed,

only a has an upper limit of 200

the program needs to output the following triple for a == 200: (200 ,
609,641)

so at this stage my problem is: how to set the upper limit for b in a
smart way?

My solution below is not efficient i believe.

import math
for a in range (190,200):
for b in range (a,a*a):
csqrd = a * a + b * b
csqrt = math.sqrt(csqrd)
for c in range (1, csqrd):
if c * c == a * a + b * b and math.floor(csqrt) == csqrt:
print (a,b,c)
 
M

MRAB

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".
a^a + b^b = c^c is the condition to satisfy and i need to use loops

Well, I'd interpret it to mean that

a<= 200
AND
b<= 200

since c is the hypotenuse, which by definition is longer than either of
the sides.

The brute force approach is a nested set of "for" loops, running
1-200 (remember that (x)range(200) runs 0-199<G>).

The alternative is to study the information at

http://www.math.uic.edu/~fields/puzzle/triples.html

and filtering out entries where the first two components are>200...
Looking at the middle term "2*m*n" would have to be less than 201, and
the first term "n*n-m*m"< 201

In Python, this can all be done in a one-liner...

[(n*n - m*m, 2*m*n, n*n + m*m) for n in xrange(2,101) for m in
xrange(1,n) if 2*m*n< 201 and n*n-m*m< 201]

Converting this to a clean set of nested loops and nice lines of
output is a different matter.

Oh, and DO study the link I gave so you can cite it when you turn in
this intriguing formulation... after all, no need for math.sqrt<G>

Hi Wulfraed,

only a has an upper limit of 200
Really? The quote you gave included "whose small sides are no larger
than n". Note: "sides", plural.
 
M

Mel

MRAB said:
On 22/10/2010 13:33, Baba wrote:
Really? The quote you gave included "whose small sides are no larger
than n". Note: "sides", plural.

Strangely, there does seem to be a limit. Fixing one side at 200, the
largest pythagorean triple I have found is (200, 9999, 10001.0). So far my
math has not been up to explaining why.

Mel
 
M

Mel

Mel said:
Strangely, there does seem to be a limit. Fixing one side at 200, the
largest pythagorean triple I have found is (200, 9999, 10001.0). So far
my math has not been up to explaining why.

Of course. Sorry. The difference between 10001**2 and 10002**2 exceeds
200**2. So adding a**2 can never even get as far as the next integral
square.

Mel.
 
L

Lawrence D'Oliveiro

In message
Baba said:
csqrt = math.sqrt(csqrd)
for c in range (1, csqrd):
if c * c == a * a + b * b and math.floor(csqrt) == csqrt:
print (a,b,c)

Is there such a term as “bogosearch�
 
D

Dennis Lee Bieber

said:
only a has an upper limit of 200
No where in your original post did you state "a" is limited... The
qualifier was "sides are no larger than n"... "Sides" implies neither is
permitted to exceed 200 -- that's why I commented that "c" is the
hypotenuse and expected to be longer than both of the sides.
 
B

BartC

Tim Roberts said:
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.

Or possibly a**2 + b**2 = c**2
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.

If square roots have to be used, you might as well use the two-loop
algorithm, as you're nearly there.

A simpler estimate for the largest c is just a+b, although it might involve
a few extra iterations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top