We have all learned in plane geometry classes the Pythagorean theorem: a squared plus b squared equals c squared. Thus, if we know two sides of a right triangle, we can easily calculate the third. Or can we?
My son pointed out a Basic program for finding “Pythagorean triples” in his geometry textbook (Jurgensen, Donnelly, Maier, and Rising. Geometry. Boston: Houghton Mifflin Co., 1978). These are the solutions to a right triangle when all three sides are positive integers. I translated the program into ZX81 Basic as follows:
10 FOR A=1 TO 20
20 FOR B=A TO 20
30 LET C=SQR(A*A+B*B)
40 IF C<>INT C THEN GOTO 60
50 PRINT A;TAB 5;B;TAB 10;C
60 NEXT B
70 NEXT A
Then we ran the problem, and the display appeared as follows:
8 15 17 0/70
This result was a surprise. What had happened to the famous 3, 4, 5 triangle, let alone all the other solutions we were sure were there? It was obvious from the display that the program was working and that it had exhausted all possible values of A up to 20, stopping at line 70.
The problem had to be in the conditional test in line 40 which says in effect, “PRINT only solutions in which C is an integer.” Some of the obvious solutions must be failing the test.
We decided to see what was happening by going to the immediate mode, i.e., entering text without line numbers, and typed:
LET A=3
LET B=4
LET C=SQR(A*A+B*B)
PRINT C
The screen display showed 5, which is an integer, or is it? Knowing that some algorithms, such as SQR, produce round-off errors, we next tried:
PRINT C-INT C
The number 1.8626452E-9 appeared. Here was the problem. A tiny error, outside the display range of the ZX81 had caused many solutions to fail the conditional test of line 40, which demands that INT C be exactly equal to C. The solution then was to rewrite line 40 to permit small errors in the SQR solution for C:
40 IF C-INT C>0.000001 THEN GOTO 60
When we ran the problem with this change, we got the following results:
3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 12 16 20 15 20 25 0/70
These are the proper solutions for all Pythagorean triples for sides A and B less than or equal to 20.
ZX81 users must be prepared to accept slight imperfections when the computer is dealing with non-arithmetic functions. The algorithms used are powerful, but some round-off errors may be present.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.