PiDP-8/I SoftwareArtifact Content
Not logged in

Artifact 5bac8ab3e8dbee65fda2f064f9d98c61b7c504c4:

Wiki page [PEP001.BA] by tangent 2019-11-01 12:20:19.
D 2019-11-01T12:20:19.031
L PEP001.BA
N text/x-markdown
P 5a9b9ee82170e7081db8090fcdf06d35f9d4030d
U tangent
W 4685
# 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](/wiki?name=Getting+Text+In).

The BASIC implementation of a program to solve [Project Euler Problem #1](https://projecteuler.net/problem=1) turned out to be surprisingly difficult to write for this old [Applesoft BASIC](https://en.wikipedia.org/wiki/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:

```basic
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
70 NEXT I
80 PRINT "TOTAL: "; T
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](https://en.wikipedia.org/wiki/Significand) 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](https://en.wikipedia.org/wiki/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](/wiki?name=PEP001.PA).


## 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:

```basic
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
40 NEXT I
50 PRINT "TOTAL: "; T
```

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](https://en.wikipedia.org/wiki/Structured_programming), it at least has no `GOTO`s 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:

*   [C](/wiki?name=PEP001.C)
*   [FORTRAN II and IV](/wiki?name=PEP001.FT)
*   [U/W FOCAL](/wiki?name=PEP001.FC)
*   [PAL8 Assembly](/wiki?name=PEP001.PA)

You may find it interesting to compare their solutions.


## License

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

[sl]: https://tangentsoft.com/pidp8i/doc/trunk/SIMH-LICENSE.md
Z 97b4791ba57e163a91cf1982d852220c