Just for fun, I have been playing with a QBasic program that generates
"Pythagorean triples" - sets of three integers, A, B, and C, such that
A^2 + B^2 = C^2. Obviously, these could be the lengths of the sides of
a right-angled triangle in which all the lengths are integers.
There are a couple of other "rules of the game". The program must *not*
output triples which are multiples of other valid triples, such as
6,8,10, which is just twice 3,4,5. Also, the triples must be output in
order, sorted by increasing A (the shortest side of the triangle), and
then by increasing B (the mid-length side).
The following version is the latest. It will generate all 1457 triples
in which A<=1000 in just a few seconds. (The limiting value for A is
the number L, which is set at the beginning of the program.) I have run
it as far as A=10,000, but by that point it gets boringly slow. By
then, 19,271 triples have been output.
Anyway... Does anyone here have anything much faster?
dow
----------------------------------------------------
' pythagorean triples
DEFLNG A-Z
L = 1000 ' limiting length of shortest side
DIM B(1 TO SQR(L)), C(1 TO SQR(L))
P = 0
CLS
FOR A = 3 TO L
Z = 0
IF A MOD 2 THEN
FOR D = 1 TO SQR(A) STEP 2
E# = A / D
E = E#
B = (E * E - D * D) \ 2
GOSUB XX
NEXT
END IF
FOR D = 1 TO A \ 2 STEP 2
E# = SQR(A + A + D * D)
E = E#
B = D * E
GOSUB XX
NEXT
FOR S = Z - 1 TO 1 STEP -1
FOR T = 1 TO S
IF B(T) > B(T + 1) THEN
SWAP B(T), B(T + 1)
SWAP C(T), C(T + 1)
END IF
NEXT
NEXT
FOR S = 1 TO Z
P = P + 1
PRINT P, , A, B(S), C(S)
NEXT
NEXT
END
XX:
IF E = E# THEN
IF B > A THEN
IF E MOD 2 THEN
U = B
V = A
DO WHILE V > 1
W = U MOD V
U = V
V = W
LOOP
IF U = 1 OR V = 1 THEN
C = (E * E + D * D) \ 2
Z = Z + 1
B(Z) = B
C(Z) = C
END IF
END IF
END IF
END IF
RETURN
------------------------------------------