PiDP-8/I SoftwarePEP001.BA
Not logged in

An OS/8 BASIC Implementation of Project Euler Problem #1

Before you begin, you may wish to start with the companion article, Getting Text In.

The BASIC implementation of a program to solve Project Euler Problem #1 turned out to be surprisingly difficult to write for this old Applesoft BASIC hand. There are a number of differences between these two dialects of BASIC. There are also some surprising limitations in the OS/8 dialect relative to Applesoft BASIC.

Here is the code I came up with:

10 FOR I = 1 TO 999
20 A = I / 3 \ B = I / 5
30 IF INT(A) = A GOTO 60
40 IF INT(B) = B GOTO 60
50 GOTO 70
60 T = T + I
90 END

A surprising aspect of this implementation of BASIC is that it seems to be using floating-point numbers for integer calculations such as this. Since the mantissa is 6 significant digits in this implementation of BASIC, it means we effectively get full integer precision for our calculation, since the answer is less than 7 digits. (And that's all I'm going to say about the answer here.)

(Surprised? JavaScript was designed the same way, such that you need either unusual cleverness in the JavaScript engine or nonstandard extensions like TypeScript to force use of an integer data type instead of a truncated floating point value.)

That feature means I didn't need the dirty subtotaling trick I used in the PAL8 assembly version.

Limitations Making the Program Longer

  1. OS/8 BASIC only accepts a GOTO as the "then" clause of IF. In fact, you can also write IF condition GOTO xx as IF condition THEN xx; both mean the same thing.

  2. You can't have multiple NEXT statements for a given FOR statement; it doesn't happen in this program, but this limitation can require you to use a GOTO to jump to the single line containing the NEXT statement.

  3. The END statement is not optional. OS/8 BASIC will flatly refuse to run the program without it.

  4. You can chain expressions together in an IF statement, but OS/8 basic does not consider a relation as an expression. It only considers simple arithmetic as an expression. Thus, we must test the two conditions in separate IF statements.

  5. OS/8 BASIC only supports AND for testing multiple conditions in a single IF statement; there is no OR.

Applesoft BASIC — and in fact, several other microcomputer BASICs from the late 1970s — didn't have any of these limitations, allowing the program to be a lot shorter and clearer:

10 FOR I = 1 TO 999
20 A = I / 3 : B = I / 5
30 IF INT(A) = A OR INT(B) = B THEN T = T + I

We're down to 5 lines, a length I feel is exactly right for this problem's proper solution.

While it isn't exactly structured programming, it at least has no GOTOs and only a single NEXT.

Further Limitations Not Affecting Program Length

Error reportage is typical of its era, which is to say quite primitive, even by mid-1970s microcomputer standards. I tried to structure the above program with two NEXT statements, one for the A and B code paths, which yielded the super-helpful NF error, which you are meant to understand means NEXT without FOR. And never mind that it's misleading, since of course there is a FOR statement, right there on line 10.

Just as with the PAL8 assembly language version, OS/8 BASIC has no modulo operator, so I had to resort to floating-point arithmetic with truncation to detect an even division. This makes me uneasy, but it gives the right answer, so clearly I have enough precision to pull this off, at least for the 1..999 case.

I also noticed that OS/8 BASIC uses the backslash character instead of a colon for multiple statements on a line, whereas microcomputer BASICs of the late 1970s typically used the colon instead.

Other Programs in the Series

I've solved this same problem in many other languages available for the PiDP-8/I:

You may find it interesting to compare their solutions.


Copyright © 2017 by Warren Young. This document is licensed under the terms of the SIMH license.