Published by Dimitris Kougioumtzis · Updated
Repeatedly divide by each candidate factor.
Divide out successful trial factors repeatedly; any remainder above one after the square-root cutoff is prime.
When to use Prime factorization (trial division)
Factoring small integers and deriving divisor functions.
Choosing an alternative
A sieve amortizes work over many candidates.
Worked example: input and state changes
Example input
84
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [], "active": [] }Step 1: Extract factor 2; remaining quotient 42.
State at this checkpoint
{ "values": [ 2 ], "active": [ 0 ] }Step 3: Extract factor 3; remaining quotient 7.
State at this checkpoint
{ "values": [ 2, 2, 3 ], "active": [ 2 ] }Step 4: Remaining factor 7 is prime.
State at this checkpoint
{ "values": [ 2, 2, 3, 7 ], "active": [ 3 ] }Step 5: Prime factors: 2 × 2 × 3 × 7.
State at this checkpoint
{ "values": [ 2, 2, 3, 7 ], "active": [] }
Expected final state
Prime factors: 2 × 2 × 3 × 7.
Complexity and input bounds
Family: Number theory. Time complexity: O(√n).
Space complexity: O(log n) factor output; O(1) working state.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
One integer from 2 to 9999.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Retain factor multiplicities and do not discard the final prime remainder.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 | // n: 2–9999. Writes prime factors, including repeated factors, and returns their count. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(n, output) { let count = 0; for (let p = 2; p * p <= n; p++) { while (n % p == 0) { output[count++] = p; n = Math.floor(n / p); } } if (n > 1) { output[count++] = n; } return count; } |
Source
No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.
- The Art of Computer Programming, Volume 2: Seminumerical Algorithms (3rd edition)
Donald E. Knuth (1997). Reference book.