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.

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
 
A

Alex Hall

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

Terry Reedy

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.

This is not well worded. I take 'small sides' (plural) to mean the two
smaller, non-hypotenuse sides (which are necessarily shorter than the
hypotenuse). So the possible pairs of values i,j, where i is the shorter
of the two, have
Try it with n<= 200."

Again, not well worded; I believe this is meant to be n==200, except
that the program should take n as a parameter and then give it value
200, so that the could work if n were given some other value.

So the possible pairs of values i,j, where i is the shorter of the two,
have j <= n (==200) and i <= j.
 
B

Baba

This is not well worded. I take 'small sides' (plural) to mean the two
smaller, non-hypotenuse sides (which are necessarily shorter than the
hypotenuse). So the possible pairs of values i,j, where i is the shorter
of the two, have


Again, not well worded; I believe this is meant to be n==200, except
that the program should take n as a parameter and then give it value
200, so that the could work if n were given some other value.

So the possible pairs of values i,j, where i is the shorter of the two,
have j <= n (==200) and i <= j.

Hi Terry,

the full exercise reads as follows:

Write a program that generates all Pythagorean triples whose small
sides are no larger than n. Try it with n <= 200. (Hint: Use two for
loops to enumerate
possible values for the small sides and then test to determine whether
the result
is an integral square.

source: http://users.soe.ucsc.edu/~pohl/12A/ch03-state.pdf (exercise
11, it's a Java book but i like to use Python for solving such basic
exercises as it is a much less cumbersome language)

i agree that possibly the wording makes this more difficult than it
is. Anyway, i'm a beginner so my problem solving techniques are still
quite shaky.

the bit i'm having difficulties with in constructing my loops is:
"whose small sides are no larger than n"
i don't know what to do with that :(

Baba
 
T

Terry Reedy

the bit i'm having difficulties with in constructing my loops is:
"whose small sides are no larger than n"

from math import sqrt

def py_trips(n):
for b in range(4,n+1):
for a in range(3,b+1):
cf = sqrt(a*a+b*b)
c = int(cf)
if c == cf: yield a, b, c

for t in py_trips(200): print(t)

# prints
(3,4,5)
....
(150, 200, 250)

This version assumes that if a*a+b*c is an exact square of an integer,
the floating point sqrt will be an exact integral value, which I believe
it should be for values up to the max (for n max 200) of 80000.

It produces multiples of each triple, such as (3,4,5), (6,8,10),
(9,12,15), ... (150,200, 250), which a different formulation of the
problem might exclude, to only ask for 'basic' triples of relatively
prime numbers.
 
B

Baba

from math import sqrt

def py_trips(n):
   for b in range(4,n+1):
     for a in range(3,b+1):
       cf = sqrt(a*a+b*b)
       c  = int(cf)
       if c == cf: yield a, b, c

for t in py_trips(200): print(t)

# prints
(3,4,5)
...
(150, 200, 250)

This version assumes that if a*a+b*c is an exact square of an integer,
the floating point sqrt will be an exact integral value, which I believe
it should be for values up to the max (for n max 200) of 80000.

It produces multiples of each triple, such as (3,4,5), (6,8,10),
(9,12,15), ... (150,200, 250), which a different formulation of the
problem might exclude, to only ask for 'basic' triples of relatively
prime numbers.

Hi Terry

Only a has an upper limit of 200. The exercise is n ot clar about that
i agree but assuming i am correct my program would need to be able to
generate the following triple: (200 ,609,641 )

My code below does that now but i think the way i compute b's upper
limit is not efficient.

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

thanks
Baba
 
B

Baba

from math import sqrt

def py_trips(n):
   for b in range(4,n+1):
     for a in range(3,b+1):
       cf = sqrt(a*a+b*b)
       c  = int(cf)
       if c == cf: yield a, b, c

for t in py_trips(200): print(t)

# prints
(3,4,5)
...
(150, 200, 250)

This version assumes that if a*a+b*c is an exact square of an integer,
the floating point sqrt will be an exact integral value, which I believe
it should be for values up to the max (for n max 200) of 80000.

It produces multiples of each triple, such as (3,4,5), (6,8,10),
(9,12,15), ... (150,200, 250), which a different formulation of the
problem might exclude, to only ask for 'basic' triples of relatively
prime numbers.

Hi All,

Only 'a' has an upper limit of 200. The exercise is not clar about
that
maybe but assuming i am correct my program would need to be able to
generate the following triple: (200 ,609,641 )


My code below does that now but i think the way i compute b's upper
limit is not efficient.


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


thanks
Baba
 
P

Peter Pearson

from math import sqrt

def py_trips(n):
for b in range(4,n+1):
for a in range(3,b+1):
cf = sqrt(a*a+b*b)
c = int(cf)
if c == cf: yield a, b, c
[snip]

Is it important to let "a" range all the way up to b, instead of
stopping at b-1? (tongue in cheek)
 
T

Terry Reedy

Makes no difference. :)

The difference is that before one writes the restricted range, one must
stop and think of the proof that a==b is not possible for a pythagorean
triple a,b,c. (If a==b, c==sqrt(2)*a and the irrationality of sqrt(2)
implies that c is also irrational and therefore not integral). The OP
asked for how to translate the problem description into two loops, one
nested inside the other, and I gave the simplest, obviously correct,
brute-force search answer.

If the problem specification had asked for primitive triples (no common
factors), an additional filter would be required.

Another respondent referred, I believe, to Euclid's formula
https://secure.wikimedia.org/wikipedia/en/wiki/Pythagorean_triple
However, it is not well suited to the problem specified.
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top