Prime Number Checker

Determine if any integer is prime using a deterministic Miller–Rabin primality test. Get prime factorization, find the next prime, and explore the fascinating world of prime numbers.

Accepts integers up to 1018 (safe 64-bit range). Larger values will use probabilistic Miller–Rabin.
? 97 (prime)
? 1000003 (prime)
? 1000000007 (prime)
⚙️ 123456789 (composite)
✨ 524287 (Mersenne prime)
? 4294967297 (Fermat number F5)
Privacy first: All calculations are performed locally in your browser. No data is sent to any server. Your inputs remain entirely on your device.

What is a Prime Number?

A prime number is a natural integer greater than 1 that has no positive divisors other than 1 and itself. The fundamental theorem of arithmetic states that every integer greater than 1 can be uniquely represented as a product of primes. This makes primes the "atoms" of arithmetic — the building blocks of all numbers. Euclid's ancient proof (c. 300 BCE) demonstrated that there are infinitely many primes, a result that still resonates across modern mathematics.

A number \( n > 1 \) is prime if for every integer \( d \) with \( 1 < d < n \), \( d \nmid n \).

Equivalently: \( \tau(n) = 2 \), where \( \tau(n) \) is the number of positive divisors.

Primality Testing Algorithms Used Here

This tool implements a deterministic Miller–Rabin primality test for 64-bit integers (up to 264), extended with a probabilistic fallback for larger BigInt values. Miller–Rabin is based on Fermat's little theorem and efficiently detects composite numbers with high certainty. For numbers below 264, we use a fixed set of bases that provably eliminate false positives: [2, 325, 9375, 28178, 450775, 9780504, 1795265022]. This deterministic base set is based on the work of Jaeschke (1993) and Sinclair (2011). For larger inputs, the test runs with 12 fixed bases providing an error probability below 2-24 — astronomically reliable for practical purposes.

The factorization module uses trial division by cached primes up to 1,000,000. For any remaining cofactor greater than 1, a Miller-Rabin test is applied to determine if it is prime or composite. For numbers smaller than 1012, full factorization is typically displayed; for larger numbers, we show discovered factors and report the primality status of the remaining cofactor.

Why Use an Interactive Prime Checker?

  • Cryptography: RSA, ECC, and Diffie-Hellman rely on large primes. Test potential prime candidates instantly.
  • Competitive Programming: Validate primality for algorithmic challenges.
  • Educational Exploration: Investigate Mersenne primes, Fermat numbers, twin primes, and prime gaps.
  • Number Theory Research: Quickly factor or verify primality of integers up to 1018.

Mathematical Foundation & Implementation

The Miller–Rabin test works by writing \( n-1 = d \cdot 2^s \) and checking whether \( a^d \equiv 1 \pmod{n} \) or \( a^{d \cdot 2^r} \equiv -1 \pmod{n} \) for some \( r \). If neither holds, \( n \) is composite. For 64-bit numbers, the deterministic base set above guarantees correctness. This implementation follows the guidelines from NIST FIPS 186-5 (Appendix C) for probabilistic primality testing. For larger BigInts, we implement modular exponentiation via exponentiation by squaring.

Factorization is performed by trial division using a cached sieve of Eratosthenes for primes up to 1 million. If a cofactor remains after trial division, its primality is determined via Miller-Rabin. The next prime function increments the input until a prime is found, using the same Miller–Rabin test with early exit.

Step-by-Step Usage

  1. Enter a positive integer in the input field (or use one of the example buttons).
  2. Click Test Primality to get primality verdict, prime factors, and detailed output.
  3. Click Find Next Prime to compute the smallest prime greater than the current number.
  4. Use Copy Results to export the data.
  5. Review the Algorithm Log to see the exact steps performed.

Case Study: RSA Key Generation Prototype

Practical Application in Cryptography

RSA encryption requires two large random primes (typically 1024–4096 bits). Using a deterministic primality test ensures the generated numbers are genuinely prime, preventing catastrophic factorization attacks. Our Miller–Rabin implementation with high certainty mirrors industrial standards (e.g., OpenSSL, cryptlib).

Step-by-step example:

  1. Use this tool to verify that 1000003 and 1000033 are prime.
  2. Compute n = 1000003 × 1000033 = 1000036000099 (a 13-digit semiprime).
  3. This simulates the first step of RSA key generation, where large primes p and q are multiplied to form the public modulus n.
  4. In practice, primes are hundreds of digits long, but the principle is identical.

This tool allows students and developers to prototype and understand the fundamental building blocks of public-key cryptography.

Prime Number Properties & Records

Category Example / Value Notes
Smallest prime 2 Only even prime
Largest known prime (2026) 2136279841 - 1 Mersenne prime, 41,024,320 digits (GIMPS)
Twin primes (41, 43), (107+ 1, 107+3) Infinitely many conjectured but unproven
Prime gap below 1018 1550 See OEIS A005250
Fermat prime 65537 Largest known Fermat prime, used in RSA
Prime Number Theorem

The Prime Number Theorem (PNT) states that the density of primes around \( x \) is approximately \( \frac{1}{\ln x} \). Formally, \( \pi(x) \sim \frac{x}{\ln x} \), where \( \pi(x) \) counts primes ≤ x. This tool helps verify prime occurrence empirically by testing consecutive numbers. For example, near 1,000,000, roughly 1 in 14 numbers is prime, aligning with 1/ln(10^6) ≈ 1/13.8.

Frequently Asked Questions

No, by definition primes must be greater than 1 and have exactly two distinct divisors. 1 has only one divisor. This convention is crucial for the fundamental theorem of arithmetic.

For deterministic testing of 64-bit integers, Miller–Rabin with fixed bases is fastest. For arbitrary precision, AKS runs in polynomial time but is slower. Our tool balances speed and accuracy, using optimized deterministic Miller-Rabin for 64-bit and probabilistic for larger numbers.

Factoring large semiprimes is computationally hard (basis of RSA). Our tool factors efficiently for numbers ≤ 1012; for larger inputs we perform trial division and report the remaining cofactor's primality. For factorization of very large numbers, specialized algorithms (GNFS) and significant computational resources are required.

For numbers < 264, it's deterministic (100% accurate). For larger BigInts, the error probability is less than 2-24 (one in 16 million) per test, making it suitable for all practical applications. This exceeds the reliability standards used in cryptographic library implementations.

Factorization underpins cryptography, algorithmic number theory, and computational mathematics. It's used in hash functions, coding theory, and digital signatures. The difficulty of factoring large semiprimes is the foundation of RSA encryption security.

Yes. All computations occur locally in your browser. No input or result data is transmitted to any server. This tool functions entirely client-side, ensuring complete privacy for sensitive numbers (e.g., cryptographic candidates).

We welcome feedback and bug reports. Please contact us via our contact page. This tool is regularly validated against established mathematical software (SageMath, Mathematica) and the Prime Pages database.
References & Verification Sources: MathWorld: Primality Test; Cormen et al. "Introduction to Algorithms" (CLRS); Miller–Rabin Wikipedia; NIST FIPS 186-5; OEIS A000040 (Prime Numbers). Deterministic bases for 64-bit: Jaeschke (1993). This implementation is regularly cross-checked with SageMath and the Prime Pages.