# Need a 12-Digit Prime?

# Need a 12-Digit Prime?

Join the DZone community and get the full member experience.

Join For Free**Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.**

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.

**Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub. Join the discussion.**

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 }}