views:

114

answers:

5

Hi everyone

i need a quick 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 "no longer than n" all about ??

exercise source: Java by Dissection (Ira Pohl and Charlie McDowell)

note: i found what looks to be a very good post on pythagorean triples but i won't read it yet as it might spoil my attempt to solve this myself....

EDIT

if n is length of the small side a and we say: n is 5; then i need to check all triples with a=1,a=2,a=3,a=4,a=5 and find the cases that are Pythagorean triples

what is this extra condition good for?

EDIT 2

maybe i'll get closer if i show you a practical piece...so here is a short piece of (python) code that returns a few triples. I've set the upper limit for the outer loop to 20 (for now i can't see any other use for 'n') to keep it managable for the post.

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

+1  A: 

The question simply means that if we assume 'a', 'b' and 'c' as sides of triangle and 'c' is hypotenuse, then 'a' and 'b' both should be less than 'n'.

i.e. a <= n and b <= n

Sachin Shanbhag
"small sides" -- I would interpret 'a' as the small side.
Jason S
You should fix your inequalities, they're definitely "<=" not "<". (3 is no larger than 3, so equality satisfies the requirement.)
Jason S
i agree with Jason, 'n' refers to 'a' as the small side of each triple
Baba
Although it is somewhat ambiguous, I interpret the question to mean both a and b are no bigger than n. It is just an exercise - try it both ways.
Keith Randall
@baba - just a thought, you do not know a,b and c. But while coding, you can assume a to be less than n, but what about b? If there is no limit set on b, it just can be anyvalue. that is why I think the condition says, your a and b both need to be less than n. This is to define a definite boundary to your results.
Sachin Shanbhag
+1  A: 

Pythagorean triples are the integer sides of a right triangle. The small sides of the triangle are the sides that form the right angle (meaning not the hypotenuse).

no larger than n means you are given an integer n and must generate all possible triples of integers a b c such that a <= n, b <= n and a^2 + b^2 = c^2.

IVlad
ok so if i take the triple (3,4,5) means that n is in range 1 to 3?what would be the point of n? or put differently: does it have a purpose beyond the exercise itself? I think Richard explains this above...need to think more about it but thanks in the meantime
Baba
The problem is that the question is somewhat ambiguous. The plural "s" in "small sides" may refer to one small side for each of the plural Pythagorean triples.
Jason S
@Baba: no, for your example (3,4,5) n is in the range 1 to 4, as the two smaller sides are both less than or equal to 4.
Matt Ellen
I'll follow Silent Ghosts advice and close this post. Thanks all for your answers. I think i need to restate my question differently as i seem to turn in circles still :(
Baba
A: 

There will be only a finite number of PTs that exist where the longest side is less than 200 "units", therefore you can iterate through each side of the three sides, the list of integers from 1 to 200 (with some basic tests to speed the process - that's the exercise) - if they are a PT - then you've found one ( remember to ignore dupes).

Richard Green
that's the brute force method. there's an efficient way to do this.
Jason S
A: 

Pythagorean triples can be generated automatically using a fairly simple formula. Here are some web pages that discuss:

Also, your question about clarifying "whose small sides are no larger than n". Suppose the triple is (A,B,C) where A < B < C. (C is the hypoteneuse, A is the shorter side, B is the longer side)

Then I would interpret the requirement as finding all triples such that A <= n. (B and C could be greater than n). The question should have been more explicit and said "shortest side" ("shortest" is better than "smallest") or explicitly called out A and/or B.

Jason S
A: 

There are an infinite number of Pythagorean triples. Therefore, if you do not place bounds on the set of triples to generate, a program cannot complete the task in finite time. So we have to bound the desired output somehow. There seems to be disagreement on whether the supplied bound applies to the shortest leg or to both legs. Here, we show that bounding the shortest leg implies a bound on the other leg.

We may take a <= b < c. Since we know sqrt(2) is irrational, we can eliminate the possibility that a = b, leaving a < b < c. Since in a Pythagorean triple we have a^2 + b^2 = c^2 and a is not zero, c >= b+1 (i.e. c is at least as big as the smallest thing that c could be). Taking c to be as small as this bound, we get a^2 + b^2 = c^2 >= (b+1)^2 and this implies a^2 >= 2b+1 or b <= (a^2 - 1)/2.

So, a bound on a is also a bound on b (and therefore c). In detail, if we require a <= n, then we have required b <= (n^2 - 1)/2. We can deduce further that c^2 <= n^2 + (n^2 - 1)^2/4.

The bound on c is pretty loose, so I wouldn't recommend looping on c then filtering out too large triangles.

Eric Towers