Skip to main content

Prime Number Checker

Check if a number is prime and find all primes up to any limit.

Reviewed by · Last reviewed

Using the Prime Number Checker

  1. Pick a mode: the Check a Number tab tests a single integer for primality; the Find Primes Up To N tab lists every prime below a ceiling you choose.
  2. Type your number in the input. Check mode accepts any positive integer up to 253-1; sieve mode accepts up to 10 million so the UI stays responsive.
  3. Submit with Enter or the button. In check mode you get a yes/no result and, if the number is composite, its prime factorisation. In sieve mode you get the full list plus the count of primes found.
  4. Scan the factorisation to see which small primes divide your number; this is often more useful than the yes/no answer.
  5. Copy the list with the Copy All button; the output is comma-separated and pastes cleanly into a spreadsheet or a code snippet.

Under the Hood: Trial Division and the Sieve

For a single check the tool uses wheel-factored trial division: it divides by 2 and 3 first, then by numbers of the form 6k±1 up to √n. That eliminates multiples of 2 and 3 from the candidate list and gives a roughly 3x speedup over plain trial division. The algorithm is deterministic and correct for any input that fits in a JavaScript number.

For the sieve, the tool implements Eratosthenes' sieve in a Uint8Array sized to N+1 bytes. It marks 0 and 1 as composite, then for every prime p found starts crossing off multiples from p² (earlier multiples were already crossed off by smaller primes). This runs in O(N log log N) time and O(N) space. For very large N a segmented sieve or the Miller-Rabin probabilistic test would scale better, but trial division and classical Eratosthenes are exactly right for the range this tool supports.

When You Need a Prime Check

  • Verifying a prime modulus when hand-rolling a Diffie-Hellman demo for a cryptography class.
  • Generating a list of primes for a Project Euler problem or a programming contest solution.
  • Finding the prime factorisation of a product so you can simplify a fraction or reduce a radical.
  • Teaching the sieve as a first encounter with algorithmic thinking in a CS class.
  • Spot-checking that a hash-table size is prime, a common rule of thumb in hash function design.
  • Picking the next prime above a target, useful for load-balancing weights that must be coprime.

Edge Cases the Tool Handles

  • 1 is not prime. The definition requires exactly two distinct positive divisors; 1 has only itself. Skipping this convention breaks the fundamental theorem of arithmetic because unique factorisation would fail.
  • 2 is the only even prime. Every other even number is divisible by 2, so the sieve crosses them off in its first sweep.
  • Carmichael numbers like 561 and 1105 fool naïve Fermat tests but are correctly identified as composite here because trial division finds their small factors directly.
  • Large Mersenne-adjacent numbers are not a special case for this tool; 231-1 (2,147,483,647) is prime and trial division confirms it in milliseconds.
  • Inputs above 253-1 lose integer precision in JavaScript's number; for those you want a BigInt-based Miller-Rabin, not this tool.
  • Zero or negative input is rejected at the UI layer; primality is only defined for natural numbers greater than 1.

Primes, Briefly

Primes are the multiplicative atoms of the integers: every positive integer larger than 1 factors uniquely as a product of primes (the fundamental theorem of arithmetic, proved by Euclid in the Elements). There are infinitely many, as Euclid showed with his famous contradiction argument. Their distribution is irregular in detail but smooth on average: the prime-counting function π(N) is asymptotic to N/ln(N), a result proved independently by Hadamard and de la Vallée Poussin in 1896 and called the Prime Number Theorem. Wilson's theorem gives a clean but impractical characterisation: p is prime if and only if (p-1)! ≡ -1 (mod p). For primality testing the modern workhorses are Miller-Rabin (probabilistic, extremely fast) and AKS (deterministic polynomial time, announced in 2002), neither of which is needed at the scales this tool targets.

When to Reach for Something Heavier

For primes beyond 10 million, segmented sieves implemented in C (primesieve) or Python with numpy vectorisation will beat a browser sieve by two orders of magnitude. For primality testing on numbers with hundreds of digits - the scale that matters for RSA key generation - you need Miller-Rabin with a curated set of witnesses, which OpenSSL and GMP ship ready to use. For fast factorisation of 20 to 60 digit composites, Pollard's rho and the elliptic curve method (ECM) in tools like YAFU or Msieve are what you want. This browser tool is the right choice when you need a fast answer for a number up to about 1015 without installing anything, and it wins on zero-install friction every time.

Frequently Asked Questions

Why is the trial-division bound exactly the square root?

If <em>n</em> has a divisor <em>d</em> greater than &radic;<em>n</em>, then <em>n</em>/<em>d</em> is a divisor less than &radic;<em>n</em>. So if trial division up to &radic;<em>n</em> finds no divisor, neither does any larger candidate, and <em>n</em> is prime. This halves the exponent of the work from <em>n</em> to &radic;<em>n</em> and is a standard first-year number theory observation.

Do you use Miller-Rabin for big numbers?

Not in this build. Miller-Rabin is the right tool for numbers with hundreds of digits, but at the range supported here (up to 2<sup>53</sup>-1 for single checks, 10<sup>7</sup> for the sieve) wheel-factored trial division and the Sieve of Eratosthenes are already deterministic, correct, and fast. Adding Miller-Rabin would require BigInt arithmetic and a curated set of witness values; a browser-based prime checker for cryptographic-scale numbers is better served by a specialised tool.

Does anything I type leave my browser?

No. The Preact tool runs the trial-division and sieve entirely in your tab; there is no network round-trip for the computation, no analytics tag attached to the number, and no persistent storage of your input. Even the Copy All button writes to the local clipboard API, not to any server.

Why is the sieve capped at ten million?

The sieve uses a Uint8Array of N+1 bytes, so a 10 million cap needs about 10 MB of memory and the whole sweep finishes well under a second. Going to 10<sup>8</sup> would allocate 100 MB and start to stress mobile devices; beyond that you want a segmented sieve that reuses a small cache-sized window. The cap is a usability decision, not an algorithmic limit.

What does it mean that 1 is excluded from being prime?

The modern definition requires exactly two distinct positive divisors. If 1 were allowed, the fundamental theorem of arithmetic - every positive integer factors uniquely into primes - would need an awkward exception because you could prepend any number of 1s to a factorisation. Nineteenth-century number theorists settled on excluding 1 precisely to keep the theorem clean.

Are twin primes (like 11 and 13) anything special in the output?

The tool does not highlight twin primes automatically, but they are easy to spot in the sieve output: any pair of consecutive list entries differing by 2. Whether there are infinitely many is still open; Yitang Zhang&apos;s 2013 bound showed infinitely many prime pairs differ by at most 70 million, and the Polymath collaboration has since sharpened that to 246 unconditionally and 6 under the generalised Elliott-Halberstam conjecture.

How does factorisation work for composite numbers?

When check mode determines a number is composite, it returns the full prime factorisation by the same trial-division pass that proved non-primality. After each small prime divides the running value, it divides it out as many times as possible (so 12 becomes 2, 2, 3 rather than 2, 6). The final quotient, if greater than 1, is itself prime and gets appended. For numbers in the supported range this is fast; for much larger composites Pollard&apos;s rho or ECM would be appropriate.

What is Wilson&apos;s theorem and why is it not used here?

Wilson&apos;s theorem states that <em>p</em> is prime if and only if (<em>p</em>-1)! &equiv; -1 (mod <em>p</em>). It is a beautiful characterisation but useless for actual primality testing because computing (<em>p</em>-1)! is exponentially more expensive than trial division, and the modular reductions do not save enough to matter. It is more often cited in proofs than in algorithms.

Is the output of the sieve sorted?

Yes, ascending, because the sieve naturally discovers primes in order as it scans the bit array from 2 upward. If you need a randomised selection for benchmarking, shuffle the output with Fisher-Yates in a separate step; the tool does not randomise for you.

Can I export or share the list of primes?

The Copy All button gives you a comma-separated list on your clipboard, ready to paste into a spreadsheet, a text file, or a code editor. There is no server-side sharing because the tool does not touch a server; if you need a public URL, paste the list into a gist or a pastebin after copying.

More Math & Calculators