Prime Number Calculator
Solve prime number problems step-by-step with our free calculator. See formulas, worked examples, and clear explanations.
Formula
Trial division: test divisors up to sqrt(n); PNT: pi(n) ~ n/ln(n)
A number n is prime if no integer from 2 to sqrt(n) divides it evenly. The Prime Number Theorem states the count of primes up to n is approximately n divided by ln(n). The Sieve of Eratosthenes efficiently finds all primes in a range by iteratively marking multiples of each prime as composite.
Worked Examples
Example 1: Testing Primality of 997
Problem: Determine whether 997 is prime and find the nearest primes.
Solution: Test 997 by trial division up to sqrt(997) = 31.6\nCheck: 997/2 = not even\n997/3: 9+9+7=25, not divisible by 3\n997/5: does not end in 0 or 5\n997/7: 997/7 = 142.4... no\n997/11: 997/11 = 90.6... no\n997/13: 997/13 = 76.7... no\n997/17: 997/17 = 58.6... no\n997/19: 997/19 = 52.5... no\n997/23: 997/23 = 43.3... no\n997/29: 997/29 = 34.4... no\n997/31: 997/31 = 32.2... no\n\n997 is PRIME (the largest 3-digit prime)\nPrevious prime: 991\nNext prime: 1009
Result: 997 is PRIME | Previous: 991 | Next: 1009 | Gap: 18
Example 2: Counting Primes Up To 100
Problem: How many primes are there up to 100, and compare with the Prime Number Theorem estimate.
Solution: Primes up to 100: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97\n\nActual count pi(100) = 25\nPNT estimate: 100/ln(100) = 100/4.605 = 21.7\nLi(100) = 30.1\n\nPNT error: |25 - 21.7|/25 = 13.2%\nLi error: |25 - 30.1|/25 = 20.4%\n\n(PNT/Li become more accurate for larger n)
Result: pi(100) = 25 primes | PNT estimate: 21.7 | Density: 25%
Frequently Asked Questions
What is a prime number and how do you determine if a number is prime?
A prime number is a natural number greater than 1 that has exactly two distinct positive divisors: 1 and itself. The first few primes are 2, 3, 5, 7, 11, 13, 17, 19, and 23. The number 2 is the only even prime because every other even number is divisible by 2. To test whether a number n is prime using trial division, check if any integer from 2 up to the square root of n divides it evenly. If none do, the number is prime. For example, to test 97: the square root of 97 is about 9.85, so we check divisibility by 2, 3, 5, and 7. Since none divide 97 evenly, it is prime. More sophisticated tests like Miller-Rabin use modular arithmetic for probabilistic primality testing of very large numbers.
How many prime numbers are there, and do they go on forever?
There are infinitely many prime numbers, a fact proven by Euclid around 300 BC with an elegant proof by contradiction. Suppose there were finitely many primes: p1, p2, ..., pk. Form the number N = p1 * p2 * ... * pk + 1. This number N is not divisible by any of the listed primes (since dividing by any pi leaves remainder 1), so either N itself is prime or N has a prime factor not in our list. Either way, we have found a prime not in the original list, contradicting our assumption. The Prime Number Theorem, proved independently by Hadamard and de la Vallee Poussin in 1896, describes the asymptotic distribution: the number of primes up to n is approximately n divided by the natural logarithm of n. This means primes become less frequent but never stop appearing.
What are twin primes and what is the twin prime conjecture?
Twin primes are pairs of primes that differ by exactly 2, such as (3,5), (5,7), (11,13), (17,19), (29,31), (41,43), and (71,73). The twin prime conjecture asserts that there are infinitely many such pairs, but this remains unproven despite centuries of effort. In 2013, Yitang Zhang achieved a breakthrough by proving that there are infinitely many pairs of primes differing by at most 70 million. Subsequent work by James Maynard and the Polymath project reduced this bound to 246. While a gap of 2 has not been proven achievable, these results represent enormous progress. Twin primes become increasingly rare among larger numbers but keep appearing. The largest known twin prime pair, as of recent records, has over 388,000 digits. The distribution of twin primes is described by the Hardy-Littlewood conjecture, which provides a specific formula for their expected frequency.
What does the Prime Number Theorem tell us about prime distribution?
The Prime Number Theorem (PNT), proved in 1896, states that the number of primes less than or equal to n, denoted pi(n), is asymptotically equal to n divided by ln(n). More precisely, the ratio pi(n) / (n/ln(n)) approaches 1 as n goes to infinity. A better approximation uses the logarithmic integral function Li(n) = integral from 2 to n of 1/ln(t) dt, which gives remarkably accurate estimates. For example, pi(1000000) = 78498, while 1000000/ln(1000000) = 72382 and Li(1000000) = 78628. The PNT implies that the average gap between consecutive primes near n is approximately ln(n), so primes near one billion are spaced about 21 apart on average. The Riemann Hypothesis, if proven true, would sharpen the error term in the PNT to the best possible bound, making prime counting extremely precise.
What is the largest known prime number?
The largest known prime number is regularly updated as new discoveries are made by distributed computing projects. As of late 2024, the record holder is 2^136279841 - 1, a Mersenne prime with over 41 million digits, discovered by the GIMPS project. Finding such enormous primes requires specialized algorithms and vast computational resources. The Lucas-Lehmer primality test, which applies specifically to Mersenne numbers, can verify whether 2^p - 1 is prime much more efficiently than general-purpose primality tests. Despite knowing these enormous primes, they represent isolated discoveries rather than a systematic enumeration. There are undoubtedly countless primes between the largest known primes that remain undiscovered. Large prime numbers have practical applications in cryptography, where RSA encryption typically uses primes with 300-600 digits, and in random number generation and hash function design.
How are prime numbers used in modern cryptography?
Prime numbers are the foundation of public-key cryptography, which secures virtually all internet communication. RSA encryption generates two large random primes (typically 1024+ bits each), multiplies them to create a public modulus, and relies on the computational difficulty of factoring this modulus back into its prime components. The Diffie-Hellman key exchange protocol uses arithmetic in groups of prime order to allow two parties to establish a shared secret over an insecure channel. Elliptic curve cryptography uses primes to define finite fields over which elliptic curve arithmetic is performed. Prime numbers also appear in hash functions, pseudorandom number generators, and error-correcting codes. The search for primes with special properties (safe primes, strong primes, Sophie Germain primes) is driven partly by cryptographic requirements. If quantum computers capable of running Shor algorithm become practical, new post-quantum cryptographic methods that do not rely on prime factorization will be needed.