Almost primes with TinyBASIC on the KIM-1 clone: PAL-1
By Michael Doornbos
- 3 minutes read - 516 wordsBill Gates has been a controversial figure in the Computer World for 50 years now.
Back in 1976, he famously (infamously?) wrote a letter bemoaning what he saw as rampant piracy of BASIC. Micro Soft was selling their version of BASIC, which is quite good for a whopping $150. This was fine for a company, but to a “tinker in your garage” person, $150 ($760 or so in 2022) was pretty steep.
It’s hard to tell which of these stories from that era are based on truth and which ones have morphed into “legend” status. Legends tend to become bigger and more important than they were at the time.
Anyway, the short version is that it fueled an effort that was already underway for several other BASIC implementations. TinyBASIC is the one we’ll be using today.
This effort was also credited with coining the term “copyleft”. I need a tattoo that says “all wrongs reserved”
TinyBASIC is an implementation of the BASIC language. It was originally specced out by Dennis Allison, published in People’s Computer Company Vol.4 No.2 and reprinted in Dr. Dobb’s Journal, January 1976.
Great, integers only, strings are second rate. Let’s code something!!
Almost primes
A k-Almost-prime is a natural number n that is the product of k (possibly identical) primes. It should be something BASIC can handle just fine.
Tiny BASIC Version
I keep finding this version on the interwebs without the line numbers in some places. I don’t know if it’s the version I’m using or how the PAL-1/KIM-1 is outputting to the terminal, but it REALLY doesn’t like lines without the numbers.
The paper tape file for the version I’m using is here if you want to give it a go.
TinyBASIC (the version I’m using) does not have FOR-NEXT loops, so we’ve got to increment and GOTO here and there instead.
So with a few modifications for formatting and adding line numbers, we have:
1 REM Almost prime
5 LET K=1
10 IF K>5 THEN END
12 PRINT "k = ";K;": ";
14 LET I=2
16 LET C=0
20 IF C>=10 THEN GOTO 40
22 LET N=I
24 GOSUB 500
25 IF P=0 THEN GOTO 30
26 PRINT I;" ";
28 LET C=C+1
30 LET I=I+1
35 GOTO 20
40 LET K=K+1
42 PRINT
45 GOTO 10
490 REM Check if N is a K prime (result: P)
500 LET F=0
505 LET J=2
510 IF (N/J)*J<>N THEN GOTO 520
511 IF F=K THEN GOTO 530
512 LET F=F+1
513 LET N=N/J
514 GOTO 510
520 LET J=J+1
521 IF J<=N THEN GOTO 510
522 LET P=0
523 IF F=K THEN LET P=-1
524 RETURN
530 LET P=0
535 RETURN
Note: the version I’m using does NOT require the LET keyword, but some of the implementations do require it, so I left it in
Extra Credit
How about porting this to Apple’s integer BASIC, which does have FOR-NEXT loops, or Commodore BASIC (see Bill Gates, we still like your version even if it wasn’t really your original idea)? If you do, let me know, I want to see it!