If you’re like me, useing the computer means doing something useful with it. That is, if I’m going to write a program, it must be something useful or I’m learning something from the experience. Over the years I’ve been able to come up with two short programs that meet these criteria. The first searches for prime numbers and the second for right triangles whose sides are integers.
The first program is one I first wrote on my BALLEY ARCADE. It worked rather well there on just a few instructions because that computer only works in integer arithematic. When I got my TIMEX, I found that it took a little more programming to convert mixed numbers into integers. The proceedure is rather simple in that it takes a number “N” and divides it by all possible integers from 2 to N/2. If none of the answers is an integer, N is recorded as a prime number. The program gives you a chance at the beginning to tell it where to stop. It then prints the number 2 (which we know is a prime), and continues to go about it’s thing. Things get pretty slow as it searches the larger numbers, since they require more calculations. My suggestion is that the program be run with the computer in the slow mode so you can watch the numbers appear as they’re computed.
5 REM A PROGRAM TO CALCULATE PRIME NUMBERS
10 PRINT "ENTER HIGHEST NUMBER TO BE USED."
15 INPUT E
20 PRINT "2 ";
25 FOR N=3 TO E STEP 2
30 LET A=2
40 LET B=N/A
42 LET D=INT B
50 LET C=D*A
60 IF C=N THEN NEXT N
70 IF A>N/2 THEN GOTO 150
80 LET A=A+1
90 GOTO 40
100 PRINT "DONE"
150 PRINT N;
155 PRINT " ";
160 NEXT N
170 PRINT "DONE"
Something that has fascinated mathematicians for some years, (well a few of us, anyway), is the fact that there exist a series of right triangles whose sides are integers; 3,4,5 for instance. Using the wisdom of Pythagoras, of A squared plus B squared equals C squared fame, I devised a routine to search out these elusive figures for me.
This program simply starts by asking you for limits on the size of the sides adjacent to the right angle, sides A and B. It then starts with A=2 and B=3 and puts them into the equation (A*A)+(B*B)=C. Next it checks to see if SQR C is a whole number. If it isn’t, A is incremented and the search continues. Whenever SQR C is an integer the three sides are printed out, A is incremented and the program continues. The whole thing is speeded up by the simple expedient of checking for the case where A = B and, when true, increment B and reset A to 2. This prevents repeating triangles such as 3,4,5 as 4,3,5.
The list of triangles is rather interesting but we still get some redundancies such as 6,8,10 which is 3,4,5 multiplied by 2. Perhaps this could be eliminated by combining part of the prime numbers program to check that at least one side is a prime number. Another possible failure in this program would occur at very large numbers, where round off error could hide the fact that SQR C is not really an integer. However, I doubt that anyone wants to investigate triangles that large.
Well, it’s been fun and I hope you enjoy these programs as much as I did.
10 PRINT "THIS PROGRAM WILL CHECK FOR ALL"
20 PRINT "THE POSSIBLE RIGHT TRIANGLES OF"
30 PRINT "SIDES A B C WHERE A,B AND C"
40 PRINT " ARE ALL INTEGERS."
50 PRINT "ENTER THE LARGEST NUMBER FOR SIDE A:"
60 INPUT F
70 PRINT "ENTER THE LARGEST NUMBER FOR SIDE B:"
80 INPUT F
81 CLS
82 PRINT "SIDE A SIDE B HYPOTENUSE"
83 LET B=3
84 LET G=0
88 LET A=2
90 LET C=(A*A)+(B*B)
100 LET H=SQR C
110 LET D=INT H
120 IF D*D=C THEN GOTO 300
130 LET A=A+1
140 IF A>B THEN GOTO 250
150 GOTO 90
250 LET B=B+1
260 IF B>F THEN GOTO 280
[illegible]70 GOTO 88
280 LET G=1
300 PRINT TAB 5;A;TAB 15;B;TAB 25;D
310 IF G=0 THEN GOTO 250
320 PRINT "DONE"
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.