Three related operations on integers: check whether a number is prime, list all primes up to a limit, or compute the prime factorisation of a positive integer. Useful in number theory homework, cryptography experiments, fraction simplification, and anywhere you need to understand a number’s prime structure.
Primality check
Given an integer n, determine whether it’s prime. The tool uses trial division up to √n with the 6k±1 optimisation: after handling 2 and 3 as special cases, every remaining prime has the form 6k+1 or 6k-1 (because any other form is divisible by 2 or 3), so the loop can skip two-thirds of the odd candidates. This runs in O(√n) time — fast enough to test any integer that fits in a JavaScript number (up to 2⁵³ ≈ 9 × 10¹⁵) in milliseconds.
Example: checking 97
Is 97 prime? Trial division: √97 ≈ 9.85, so we only need to check divisors up to 9. 97 is odd, not divisible by 3 (sum of digits = 16), not divisible by 5 (doesn’t end in 0 or 5), not divisible by 7 (97 = 13×7+6). 8 and 9 are composite (2 and 3 already checked). No divisor found → 97 is prime.
List primes up to N
Using the Sieve of Eratosthenes, the tool can list every prime up to N in a few hundred milliseconds for N up to 10 million. The sieve works by “crossing out” composites: start with 2, mark its multiples as composite; move to 3, mark its multiples; continue with each unmarked number. What remains unmarked is the set of primes. For readability, the UI truncates the displayed list at 500 entries for very large N — the full count is still shown, just not every prime.
Example: the primes ≤ 30
The sieve produces 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 — the ten primes under 30. There are 25 primes under 100, 168 under 1000, 1229 under 10,000, and 664,579 under 10 million. The density of primes thins out as numbers grow (the prime counting function π(N) approaches N/ln(N) for large N), which is called the Prime Number Theorem.
Prime factorisation
Given a positive integer ≥ 2, decompose it into prime factors with their exponents. For n = 60, the factorisation is 2² × 3 × 5. For n = 1024 = 2¹⁰. For n = 2310 = 2 × 3 × 5 × 7 × 11 (the first five primes).
The algorithm is trial division by candidate primes in order, dividing repeatedly by each prime until it no longer divides, then moving on. If the remaining cofactor is still > 1 after the trial-division loop, it’s itself a prime and gets added with exponent 1.
Example: factorising 2310
2310 ÷ 2 = 1155. 1155 ÷ 3 = 385. 385 ÷ 5 = 77. 77 ÷ 7 = 11. 11 is prime. So 2310 = 2 × 3 × 5 × 7 × 11 — the product of the first five primes (also called the primorial p#(5)).
Why primality matters
Prime numbers are the atoms of number theory: every positive integer > 1 factors uniquely into primes (the Fundamental Theorem of Arithmetic). This uniqueness is why primes show up everywhere:
- Cryptography: RSA encryption relies on the fact that factoring a large product of two primes is computationally hard. Generating RSA keys means finding big primes (typically ≥ 2048 bits).
- Fraction simplification: To simplify a/b, divide both by GCD(a, b), which is the product of shared prime factors — the GCD / LCM calculator does that directly, and the fraction calculator builds it into every operation.
- Hashing and pseudorandom number generation: Modular arithmetic mod a prime has nice algebraic properties that composite moduli lack.
- Combinatorial identities: Many counting formulas factor cleanly in terms of primes.
What this tool does not do
It doesn’t do fast industrial-strength primality testing. For cryptographic-grade primes (hundreds of digits), trial division is hopelessly slow — you need Miller-Rabin, AKS, or elliptic-curve primality proving. The tool is for integers that fit in a double (up to about 9 × 10¹⁵), which is well beyond what trial division can still handle quickly.
It doesn’t handle negative or fractional numbers. Primality is defined only for positive integers > 1. The tool rejects fractional and non-integer inputs with a parse error.
It doesn’t sieve above 10 million. Memory and UI responsiveness cap the sieve. For larger ranges use a native command-line tool.
It doesn’t show the step-by-step factorisation. For “2310 ÷ 2 = 1155, ÷ 3 = 385, …” as a visual walk-through, you’d need a dedicated educational tool. The tool returns the final factorisation as a compact p₁^e₁ × p₂^e₂ × … expression.
It doesn’t handle BigInt. JavaScript’s native BigInt type supports arbitrary-precision integers, but the tool uses regular Number throughout, so inputs are limited to the safe-integer boundary.