How many primes are there under 1234567891011?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The sieve of Erastrosthenes is the most efficient!
Note that we only need to check divisors up to 1 2 3 4 5 6 7 8 9 1 0 1 1 ≈ 1 1 1 1 1 1 1 .
I generated the sieve of Erastosthenes in "pages" of 1 1 1 1 1 1 2 numbers. For the first page, we start at p = 2 and cross out all its multiples on the page. Then we use the first (not crossed-out) number, p = 3 , and cross out its multiples. And so on. Meanwhile, we keep a table of all primes we encountered. These are all the prime numbers we need for the entire search.
We also keep a table "next", which tells where the first multiple of a prime number p will be on the next page.
For each of the (slightly more than 1.1 million) pages, we cross out all multiples of all primes previously generated. The starting point comes from the table "next", and we update the value of "next" once the page is done. All not crossed-out numbers on the page must be prime; there is no need to store them.
The last page takes special care, because we must not count all prime numbers on it.
Running this Java program took slightly more than one hour.
Output: 4 6 0 6 3 8 7 4 3 4 6 .