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
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 nmeans you are given an integernand must generate all possible triples of integersa b csuch thata <= n, b <= nanda^2 + b^2 = c^2.