This program finds and displays Pythagorean triples — sets of integers (X, Y, H) satisfying X²+Y²=H² — by iterating X from 1 to 75 and Y from X to 100. The hypotenuse candidate is computed as the square root of X²+Y², with a small epsilon offset (0.00000004) added before the integrality test to compensate for floating-point rounding errors. When a valid triple is found, it is printed and the display scrolls every two results to keep output readable, with a long PAUSE (40000 counts) between findings. The program also includes an inline SAVE command at line 9998 with a filename containing inverse-video characters, and a GOTO loop back to line 10 at line 9999.
Program Analysis
Program Structure
The program is organized into a simple sequential flow with two nested FOR loops as its core engine:
- Line
0: Title/author banner in inverse video (display only, never executed as code). - Lines
10–90: Initialization — reset counterA, clear screen, print heading. - Lines
100–200: Main computation loop — nestedFOR X / FOR Ywith triple detection and display. - Line
9997:STOPto halt after all iterations. - Line
9998:SAVE "SIDE%S"— saves the program; only reached manually or in certain flow paths. - Line
9999:GOTO 10— restart loop (unreachable in normal execution afterSTOP).
Algorithm
The program searches for integer solutions to the Pythagorean equation X²+Y²=H² by brute force. The outer loop runs X from 1 to 75, and the inner loop runs Y from X to 100 (avoiding duplicate pairs where X>Y). For each pair, line 130 computes the candidate hypotenuse:
LET H=(X*X+Y*Y)**.5+.00000004
The fractional part of H is then tested at line 140: if H-INT H > 0.0000001, the triple is rejected. If the condition is false (i.e., H is effectively an integer), the triple is printed.
Floating-Point Epsilon Technique
The small constant 0.00000004 added to the square root in line 130 is a deliberate epsilon offset. Because floating-point square roots of perfect squares may fall just below the true integer value (e.g., 4.9999999 instead of 5.0), the offset nudges the result upward so that INT H correctly rounds to the integer value. The rejection threshold of 0.0000001 in line 140 is chosen to be large enough to pass near-integer results yet small enough to reject genuinely non-integer hypotenuses. This two-constant approach is a well-known idiom for integer detection via floating-point arithmetic on 8-bit systems.
Display and Pacing
Results are printed using the comma separator in line 150 (PRINT X;" ";Y;" ";H,), which advances to the next print zone rather than a new line, allowing compact output. The counter A tracks the number of triples found; line 170 scrolls the display after every second result using the even/odd test IF A/2=INT(A/2). A PAUSE 40000 at line 180 inserts a long delay after each triple is displayed, giving the user time to read before the next result appears.
Key Variables
| Variable | Role |
|---|---|
A | Count of triples found; used to trigger SCROLL every two triples |
X | Shorter leg of the candidate triple (1–75) |
Y | Longer or equal leg of the candidate triple (X–100) |
H | Candidate hypotenuse (floating-point square root with epsilon) |
Notable Techniques and Idioms
- Starting the inner loop at
Y=Xrather thanY=1avoids printing both (3,4,5) and (4,3,5) as separate results. - The even/odd scroll trigger (
A/2=INT(A/2)) is a standard modulo-2 idiom on platforms lacking aMODoperator. - The
SAVEcommand at line9998uses an inverse-video filename (SIDE%S), where%Srepresents an inverse-video “S”. This is a cosmetic choice and does not affect the save operation functionally. - Line
0is used as a non-executing banner line. The ZX81/TS1000 will never reach line 0 from aGOTO 10, so it serves purely as onscreen documentation when the program is listed.
Content
Source Code
0 % %W%R%I%T%T%E%N% %B%Y% %G%E%N%E% %B%U%Z%A% % % %O%F% %T%H%E% %S% %A%N%D% %S% %C%O%.% %
10 LET A=0
80 CLS
90 PRINT AT 20,4;"PYTHAGOREAN TRIPLES"
100 SCROLL
110 FOR X=1 TO 75
120 FOR Y=X TO 100
130 LET H=(X*X+Y*Y)**.5+.00000004
140 IF H-INT H>.0000001 THEN GOTO 190
150 PRINT X;" ";Y;" ";H,
160 LET A=A+1
170 IF A/2=INT (A/2) THEN SCROLL
180 PAUSE 40000
190 NEXT Y
200 NEXT X
\n9997 STOP
\n9998 SAVE "SIDE%S"
\n9999 GOTO 10
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
