This program computes the logarithm of any number X to any base Y using the change-of-base formula: log_Y(X) = LN(X) / LN(Y). After displaying the result, it accepts a dummy keypress via INPUT W$ and then loops back to the start with RUN, allowing repeated calculations. Lines 9998–9999 are utility lines for saving and restarting the program and would not be reached during normal execution. The program relies entirely on the built-in natural logarithm function LN rather than any machine code or external routines.
Program Analysis
Program Structure
The program is a simple, linear routine with an implicit loop created by RUN at line 130. There are no subroutines, no GO TO branching, and no conditional logic. Lines 9998–9999 are developer utility lines placed well beyond normal execution reach.
| Lines | Purpose |
|---|---|
| 10–30 | Clear screen and display title/prompt |
| 40–50 | Input and echo the logarithm base Y |
| 60–80 | Input and echo the number X |
| 90 | Calculate the logarithm using change-of-base formula |
| 100–110 | Display the result |
| 120–130 | Wait for user input, then restart |
| 9998–9999 | SAVE utility and fallback RUN; never reached in normal flow |
Core Algorithm
The change-of-base formula is implemented directly at line 90:
LET Z = LN X / LN Y
This correctly computes logY(X) for any positive base Y ≠ 1 and any positive X, relying entirely on the built-in LN (natural logarithm) function. No lookup tables or approximations are used.
Notable Techniques
- RUN as a loop: Line
130usesRUNto restart the program from line10, clearing all variables and providing a clean loop without needing aGO TO. This is a common Sinclair BASIC idiom. - INPUT W$ as a pause: Line
120usesINPUT W$to hold the result on screen until the user presses ENTER, acting as a simple “press any key to continue” mechanism. UnlikePAUSE 0/INKEY$, this requires ENTER to proceed. - Comma positioning: Lines
20,30,60,100use double commas (,,) inPRINTstatements to advance to the third print zone, providing basic centred-ish formatting withoutATorTAB. - Echo of inputs: Lines
50and80explicitlyPRINTthe entered values back to the screen, compensating for the fact thatINPUTprompts on some Sinclair machines do not leave the entered value visible after acceptance.
Content
Source Code
10 CLS
20 PRINT ,,"LOGARITHMS OF ANY BASE"
30 PRINT ,," INPUT BASE OF NUMBER? ";
40 INPUT Y
50 PRINT Y
60 PRINT ,,"WHAT IS THE NUMBER? ";
70 INPUT X
80 PRINT X
90 LET Z=LN X/LN Y
100 PRINT ,,"LOGARITHM OF ";X
110 PRINT "IS ";Z
120 INPUT W$
130 RUN
9998 SAVE "LOGARITHM%S"
9999 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
