Prime Group Percentages

As usual, I am curious about the distribution of the primes.  This morning I wondered what percent of numbers with N digits were prime.  Not an earth shattering curiosity…  Naturally, I wrote a little perl program to answer it for me.  Here is the preamble:

This says that we are a Perl program and that we will test primality of numbers.

Next the program sets up some parameters and buckets for our numbers that have been seen.  Max is the number of integers we will look at.  N is the current integer we are inspecting (from 1 to max).

And now for the first bite of the meat of the program:

This loops over each number, “n”, deciding if the number is prime or not.

The “all” bucket is incremented for each length of “n” (the digit size).  If “n” is prime, the program increments the digit size of the “primes” bucket as well.  (That is, 3 is of length (digit size) 1, 11 is length 2, etc.)

Additionally, the current number and its digit size are printed out.  This is just for convenience, to show that “Yes, we are still running!”   But this actually slows down the algorithm…  So comment-out if desired!

With these buckets filled, the final section of the program is to compute the percentages seen, and print out the results:

This looks at each digit size of all numbers, sorted from 1 ascending.  If the size is in the “primes” bucket, then compute the percentage of times prime numbers were seen for that digit size.

And what are the results when run for all numbers below 1,000,000,000?  Well, this algorithm in Perl is slow, for one!

This means that there are 4 single digit primes out of 9 total numbers, which equals 44.44%.  There are 21 two digit primes out of 90 total, etc.

Descending, asymptotically:

This is the “Number of primes with n digits” sequence, which goes up to 22 digits, at the OEIS.

Comments

*