--- title: "Pythagorean Triples" id: 57630 type: "computer_media" slug: "pythagorean-triples" url: "http://localhost/computer_media/pythagorean-triples/" markdown_url: "http://localhost/computer_media/pythagorean-triples.md" published_at: "2024-10-06T00:57:31+00:00" modified_at: "2026-04-03T07:58:11+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/pythagorean-triples.png" excerpt: "A floating-point Pythagorean triple finder that uses clever epsilon-offset arithmetic to overcome rounding errors while scrolling results automatically on-screen." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" indiv: - name: "Gene G. Buza" slug: "gene-g-buza" taxonomy: "indiv" url: "http://localhost/indiv/gene-g-buza/" genre: - name: "Education" slug: "education" taxonomy: "genre" url: "http://localhost/type/education/" - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 56726 title: "Synchro-Sette November 1983" type: "computer_media" url: "http://localhost/computer_media/synchro-sette-november-1983/" media_type: "Program" programmers: - name: "Gene G. Buza" slug: "gene-g-buza" taxonomy: "indiv" url: "http://localhost/indiv/gene-g-buza/" mediadate: "November 1983" images: - url: "http://localhost/wp-content/uploads/2024/10/pythagorean-triples.png" media_type_tags: "Education, Mathematics" --- # Pythagorean Triples 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: 1. Line `0`: Title/author banner in inverse video (display only, never executed as code). 2. Lines `10`–`90`: Initialization — reset counter `A`, clear screen, print heading. 3. Lines `100`–`200`: Main computation loop — nested `FOR X / FOR Y` with triple detection and display. 4. Line `9997`: `STOP` to halt after all iterations. 5. Line `9998`: `SAVE "SIDE%S"` — saves the program; only reached manually or in certain flow paths. 6. Line `9999`: `GOTO 10` — restart loop (unreachable in normal execution after `STOP`). ### 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=X` rather than `Y=1` avoids 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 a `MOD` operator. - The `SAVE` command at line `9998` uses an inverse-video filename (`SIDE%S`), where `%S` represents an inverse-video “S”. This is a cosmetic choice and does not affect the save operation functionally. - Line `0` is used as a non-executing banner line. The ZX81/TS1000 will never reach line 0 from a `GOTO 10`, so it serves purely as onscreen documentation when the program is listed. ## 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 9997 STOP 9998 SAVE "SIDE%S" 9999 GOTO 10 ```