4

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

Community
  • 1
  • 1
  • 1
    `Try it with n <= 200.` doesn't help? – SilentGhost Oct 20 '10 at 09:42
  • @Baba: your question has evolved far beyond its original state. You might want to close it and start anew. – SilentGhost Oct 21 '10 at 10:40
  • Hi Silent Ghost, all answers have been great. However i feel that the statment "triples whose small sides are no larger than n." is still unclear. Where does n come from if all i have to work with is a,b,c ? –  Oct 21 '10 at 11:16
  • p.s. Eric's answer below is certainly a good hint but as it digresses into a mathematical proof of the statement "bounding the shortest leg implies a bound on the other leg." it doesn't help me as such as i am looking for a simpler response. The guys who wrote the book i took the exercise from certainly hadn't anything that complicated in mind. –  Oct 21 '10 at 11:26

5 Answers5

2

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
  • 43,099
  • 13
  • 111
  • 179
  • 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 –  Oct 20 '10 at 11:45
  • 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 Oct 20 '10 at 13:22
  • @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 Oct 20 '10 at 13:23
  • 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 :( –  Oct 21 '10 at 20:30
1

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
  • 54,530
  • 11
  • 89
  • 103
  • "small sides" -- I would interpret 'a' as the small side. – Jason S Oct 20 '10 at 13:18
  • 1
    You should fix your inequalities, they're definitely "<=" not "<". (3 is no larger than 3, so equality satisfies the requirement.) – Jason S Oct 20 '10 at 13:18
  • i agree with Jason, 'n' refers to 'a' as the small side of each triple –  Oct 20 '10 at 13:49
  • 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 Oct 20 '10 at 16:13
  • 1
    @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 Oct 20 '10 at 18:47
1

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
  • 4,175
  • 1
  • 15
  • 17
0

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
  • 2,037
  • 2
  • 20
  • 38
0

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.

dtburgess
  • 613
  • 5
  • 19
Jason S
  • 184,598
  • 164
  • 608
  • 970