What Is the First 21-Digit Prime in the Digits of e?
At a glance
- 1. Fix the digit sequence
Decide whether the sequence begins with the integer digit 2 or with the digits after the decimal point.
- 2. Scan 21-digit windows
Read each block of 21 consecutive digits as an integer, skipping blocks with a leading zero.
- 3. Test primality exactly
Use a primality test that provides a proof, then report the first prime and its starting position.
A “21-digit number in the digits of ” means a block of 21 consecutive decimal digits, read as an integer. The phrase needs one convention: do you include the initial integer digit in , or start after the decimal point? The search below uses the usual full expansion, beginning with 2.
A block is not prime just because its digits come from . Each block must be tested as an integer. The program prints the first prime it finds, as well as the zero-based position where that block begins.
Search the full decimal expansion
This SageMath program generates 500 significant digits of , checks every consecutive 21-digit block, and stops at the first prime. Sage’s is_prime(proof=True) requests a proof rather than relying only on a probable-prime test.
from decimal import Decimal, localcontext
from sage.all import ZZ
# Generate e to ample precision and remove the decimal point.
with localcontext() as context:
context.prec = 500
e_text = format(Decimal(1).exp(), "f")
digits = e_text.replace(".", "")
length = 21
for start in range(len(digits) - length + 1):
block = digits[start:start + length]
# A block beginning with zero is not a 21-digit integer.
if block[0] == "0":
continue
number = ZZ(block)
if number.is_prime(proof=True):
print("Starting position:", start)
print("First 21-digit prime:", number)
break
else:
raise RuntimeError("No prime found in the generated digits")
Run this in SageMath; the two printed lines give the answer under this convention. The index counts from the first digit 2, so position 0 begins with 2, position 1 begins with 7, and so on. The generated string has 500 digits, so there are 480 possible starting positions for a 21-digit window; the loop scans those positions in order.
Why the search checks every window
A 21-digit block beginning at position consists of the digits at positions through . The next block begins at , not : consecutive windows overlap in 20 digits. Testing only non-overlapping groups can miss the first prime.
For example, the first window under this convention is 271828182845904523536. It ends in an even digit, so it is composite. The next windows also have to be checked in order; finding one that looks unusual or has a large digit sum does not establish primality.
The program skips a block beginning with zero because an integer such as 012345678901234567890 has only 20 digits in ordinary notation. If you instead want every 21-character substring regardless of leading zero, remove that condition—but then the question is about digit strings, not 21-digit integers.
Starting after the decimal point
If “digits of ” means only the digits after the decimal point, start the search with 71828182845904523536... rather than 271828182845904523536.... In the program, replace the line that defines digits with:
digits = e_text.replace(".", "")[1:]
That changes the position numbering and may change which prime is first. State the convention whenever you report the result.
Common mistakes
- Checking only one block at a time. The windows overlap; advance the start by one digit.
- Treating a probable prime as a proved prime. Some libraries use fast probable-prime tests for large integers. Here the code requests SageMath’s proof mode.
- Counting positions inconsistently. This program includes the leading
2and numbers positions from zero. - Using too few digits. The expansion must contain at least 21 digits beyond the start of the last window being tested. The code generates substantially more than that for a practical search.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.