I was reading a recent blog post by Evelyn Lamb where she mentioned in passing that 314159 is a prime number and that made me curious how many such primes there are.
Let’s look at numbers formed from the digits of π to see which ones are prime.
Obviously 3 and 31 are prime. 314 is even. 3141 is divisible by 9 because its digits sum to 9, and 31415 is clearly divisible by 5. And now we know that 314159 is prime. What’s the next prime in the sequence? Here’s a little Python code to find out.
from sympy import pi, isprime M = 1000 digits = "3" + str(pi.evalf(M+1))[2:] for i in range(1, M+1): n = int(digits[:i]) if isprime(n): print(n)
This looks at numbers formed from the first digit up to the thousandth digit in the representation of π. The only other prime it finds is
After writing running the Python code above, I wondered whether this sequence is in OEIS, and indeed it is, sequence A005042. The OEIS says the next number in the sequence is 16,208 digits long.
Expected number of primes
I expected to find more primes out of the first 1,000 digits of π and was surprised that the next prime is so long.
How many primes whould we expect if we look at random numbers of length 1 through 1000? (The numbers formed from the digits of π are not random, but I want to compare them to random selections.)
From the prime number theorem, the probability that a d-digit number is prime is approximately 1/d log 10. So if we pick numbers of length 1, 2, 3, … N, the number of primes we’d expect to find is
where HN is the Nth harmonic number. Harmonic numbers grow very slowly, and so this gives us an idea why the number of primes we find is so small. In particular, it says if we look out 1,000 digits, we’d expect to find 3.25 primes. And so finding 4 primes isn’t surprising.
By the way, HN is roughly log N, so the expected number of primes is roughly log N / log 10, or simply log10 N. 
To see if our prediction holds, let’s try a few more irrational numbers.
First, let’s look at e. Then we get these four prime numbers:
2 271 2718281 2718281828459045235360287471352662497757247093699959574966967627724076630353547594571
Next, let’s try the square root of 2. Then we get these three prime numbers:
1414213562373095048801688724209698078569671875376948073 1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459
And finally, sin(2018) gives us two primes:
We might expect to find more primes when the leading digit is small because each time we select a number with d digits we’re looking lower in the range where primes are more dense. That is consistent with the small number of examples in this post.
 When I write “log” without a subscript, I always mean natural log, log base e. If you’re not accustomed to this, see why this is conventional.