# Need a 12-Digit Prime?

# Need a 12-Digit Prime?

Join the DZone community and get the full member experience.

Join For FreeThe open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

You may have seen the joke “Enter any 12-digit prime number to continue.” I’ve seen it floating around as the punchline in several contexts.

So what do you do if you need a 12-digit prime? Here’s how to find the smallest one using Python.

>>> from sympy import nextprime >>> nextprime(10**11) 100000000003L

The function `nextprime`

gives the smallest prime larger than its argument. (Note that the smallest 12-digit number is 10^{11}, not 10^{12}. Great opportunity for an off-by-one mistake.)

Optionally you can provide an addition argument to `nextprime`

to get primes further down the list. For example, this gives the second prime larger than 10^{11}.

>>> nextprime(10**11, 2) 100000000019L

What if you wanted the largest 12-digit prime rather than the smallest?

>>> from sympy import prevprime >>> prevprime(10**12) 999999999989L

Finally, suppose you want to know how many 12-digit primes there are. SymPy has a function`primepi`

that returns the number of primes less than its argument. Unfortunately, it fails for large arguments. It works for arguments as big as `2**27`

but throws a memory error for `2**28`

.

The number of primes less than *n* is approximately *n* / log *n, *so there are about 32 billion primes between 10^{11} and 10^{12}. According to Wolfram Alpha, the exact number of 12-digit primes is 33,489,857,205. So if you try 12-digit numbers at random, your chances are about 1 in 30 of getting a prime. If you’re clever enough to just pick odd numbers, your chances go up to 1 in 15.

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Published at DZone with permission of John Cook , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

## {{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}