Factorizing Numbers

This file is part of Timex Sinclair Public Domain Library Tape 1001 . Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 1000

This program factorizes a user-supplied integer into its prime factors, displaying the result in the form “N = 1 X f1 X f2 …”. It uses trial division, starting at 2 and then stepping through odd numbers only (achieved by setting F to 1 after the initial factor of 2 is exhausted, then always adding 2). The square root of the original number is pre-computed and stored in S to limit the search, a classic optimisation for trial division. A flag variable PF tracks whether any factor has been found so that a lone prime can be labelled “PRIME NUMBER”. The program loops back to line 5 (the REM header) after each factorisation, effectively restarting cleanly without a CLS of the REM line itself.


Program Analysis

Program Structure

The program is organised into four functional blocks, each at a distinct line-number range:

  1. Lines 5, 100–120 — title display and user input.
  2. Lines 130–250 — initialisation and the inner trial-division loop.
  3. Lines 300–340 — factor-advance logic (move to the next candidate divisor).
  4. Lines 400–460 — result display, prompt, and restart.

Lines 500–510 are a SAVE/RUN tail used for distribution; they are never reached during normal execution.

Algorithm: Trial Division

The algorithm divides the working copy N repeatedly by the current factor candidate F, printing each factor found. The original value is preserved in NN for the prime-number message and for the special-case guard at line 200.

The divisor sequence is: 2, then all odd numbers from 3 upward. This is achieved by a small trick at line 310: after exhausting the factor 2, F is set to 1 so that the unconditional LET F=F+2 at line 330 brings it to 3, and thereafter always increments by 2, skipping even candidates entirely.

The upper bound is the pre-computed square root S=SQR N (line 150), stored once at the start. This correctly limits the search because if no factor ≤ √N exists, the remaining N must itself be prime.

Key Variables

VariableRole
NWorking number, divided down as factors are found
NNOriginal input, preserved for the prime label
FCurrent trial divisor
SSquare root of original N, loop upper bound
PFPrime flag: 0 = no factor found yet, 1 = composite
A$Dummy variable to consume the ENTER keypress

Notable Techniques

  • Compound condition at line 300: IF PF=0 AND F>S OR N=1 THEN GOTO 400 — on ZX81 BASIC, AND binds more tightly than OR, so this correctly exits when either all factors are exhausted (N=1) or no factor was found within the square-root bound.
  • Output format: The print statement at line 170 uses four commas to push the output to the fifth print zone before printing the equation start, giving a centred appearance on the 32-column display.
  • Restart via GOTO 5: Jumping back to the REM line (line 5) rather than using RUN preserves the variable A$ and avoids re-initialising the system, but it effectively restarts the program flow cleanly.
  • Inverse-video REM title: Line 5 stores the program name “NUMBER FACTORS” in inverse video characters, a common ZX81 convention for self-documenting listings visible in the program editor.

Bugs and Anomalies

  • Typo in prompt: Line 110 prints "WHATS YOUR MUMBER" — “MUMBER” instead of “NUMBER”. This is a cosmetic error only.
  • Special-case guard for N=2: The condition OR NN=2 in line 200 prevents the loop from treating 2 as composite (since 2/2=1, which would trigger the factor branch incorrectly on the second pass). However, the logic is slightly fragile: it relies on NN holding the original input, which works correctly here because NN is never modified.
  • Square root not updated: S is computed from the original N at line 150 and never updated as N is divided down. This is actually correct for the purpose of bounding the search, but it means the loop may run a few extra iterations beyond the mathematically minimal bound after partial factorisation. It does not produce wrong results.
  • PF flag reset: PF is reset to 0 at line 160 on each run through the initialisation block, ensuring the prime detection is correct for successive inputs.

Content

Appears On

Assembled by Tim Ward from many sources. Contains programs 10001 – 10050.

Related Products

Related Articles

Related Content

Image Gallery

Factorizing Numbers

Source Code

   5 REM %N%U%M%B%E%R% %F%A%C%T%O%R%S
 100 PRINT "FACTORIZING NUMBERS"
 101 PRINT 
 110 PRINT "WHATS YOUR MUMBER"
 120 INPUT N
 130 LET NN=N
 140 LET F=2
 150 LET S=SQR N
 160 LET PF=0
 170 PRINT ,,,,N;" = 1";
 200 IF N/F<>INT (N/F) OR NN=2 THEN GOTO 300
 220 PRINT "X";F;
 230 LET N=N/F
 240 LET PF=1
 250 GOTO 200
 300 IF PF=0 AND F>S OR N=1 THEN GOTO 400
 310 IF F=2 THEN LET F=1
 330 LET F=F+2
 340 GOTO 200
 400 IF PF=0 THEN PRINT " X ";NN;" PRIME NUMBER"
 410 PRINT 
 420 PRINT ,,"THATS ALL"
 430 PRINT AT 21,19;"PRESS ENTER"
 440 INPUT A$
 450 CLS 
 460 GOTO 5
 500 SAVE "1004%2"
 510 RUN 

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top