--- title: "Factorizing Numbers" id: 57018 type: "computer_media" slug: "factorizing-numbers" url: "http://localhost/computer_media/factorizing-numbers/" markdown_url: "http://localhost/computer_media/factorizing-numbers.md" published_at: "2024-10-02T07:41:23+00:00" modified_at: "2026-03-30T21:20:07+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/42_Fact.png" excerpt: "A prime factorisation tool using trial division with an odd-number skip and square-root cutoff — see how the flag variable catches lone primes." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 56732 title: "Timex Sinclair Public Domain Library Tape 1001" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1001/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/42_Fact.png" media_type_tags: "Mathematics" --- # Factorizing Numbers 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 | Variable | Role | | --- | --- | | `N` | Working number, divided down as factors are found | | `NN` | Original input, preserved for the prime label | | `F` | Current trial divisor | | `S` | Square root of original `N`, loop upper bound | | `PF` | Prime 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. ## 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 ```