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

31415926535897932384626433832795028841.

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 *H*_{N} is the *N*th 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, *H*_{N} is roughly log *N*, so the expected number of primes is roughly log *N* / log 10, or simply log_{10} *N*. [1]

## Other numbers

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:

89 890078059172214077866908781516358161827277734319970800477363723960779818810729974998238099333154892009885458973321640841442042739723513347225471340149039460391024770670777037177784685508982613866838293832904866761046669825265135653209394367093522291768886320360545920897111922035021827790766895274726280695701753189405911185099453319400331979946988526902527520607778873012543871813339138882571549068827579760491442082336321409598206489746377828352552073997340874649330911864108558685738003182332758299767812686413014214285003827180623455181083706822164258525158371971877396069605035317998430446771215296978812833752733

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.

## Related posts

[1] 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.

Are there infinitely many? Would that follow from the conjecture/presumption that pi is normal?

According to OEIS, it’s unknown whether there are infinitely many such primes, but it seems reasonable. I don’t know whether it would help to know the answer to the normal number conjecture.

I do not believe that it would help to prove that pi is normal. Consider the following: start with a normal number, and for each n, if the first n digits form a prime, change the nth digit to 0. If the number of digits changed in this way has measure 0, in the limiting sense (which we expect to be the case, although it seems difficult to prove), then this process does not affect the limiting frequency of any digit sequence, and the number is still normal but now has no primes among the numbers formed from its leading digits.

How does the program work given that the int data type isn’t supposed to hold integers larger than 2^63 ~ 10^20 ?