
The 10,000,000,000th Prime Number (1994) - lelf
https://code.jsoftware.com/wiki/Doc/Articles/Play104
======
svat
(1994).

The article is a story, about how Roger Hui (who with Kenneth E. Iverson
developed the programming language J), set about finding the 10 billionth
prime: by the Prime Number Theorem it should be about 10^10*log(10^10) which
is less than 270 billion, so he split the range into 270 parts, and had each
of several machines pick up one part and sieve that billion-number range (to
count the number of primes), a million integers at a time. After 20 hours (on
60-70 workstations) he had the counts, so knew the exact million-integer range
in which the prime occurred, from which the 10 billionth prime turns out to be
either 252,097,800,629 (with 2 as the 0th prime) or 252,097,800,623 (with 2 as
the 1st prime).

After doing this, he was able to correct an off-by-one count in a table in a
printed book, but the real moral of the story is that this is of course a poor
way to compute the nth prime for large n: it has been known since the 1800s
how to quickly compute pi(x) (the number of primes not greater than x), and
using a published algorithm known at the time would have let him find the ten
billionth prime in less than 3 minutes instead of thousands of hours of
computer time.

That's the article (though I've focused more on the algorithm while it says
“this column is not so much an article about programming as it is about
computer logistics”), but these days we can just hit up
[https://www.wolframalpha.com/input/?i=the+ten+billionth+prim...](https://www.wolframalpha.com/input/?i=the+ten+billionth+prime)
or
[https://primes.utm.edu/nthprime/index.php#nth](https://primes.utm.edu/nthprime/index.php#nth)
or enter "prime(10000000000)" into
[https://live.sympy.org/](https://live.sympy.org/) (this times out for some
reason, though primepi(22801763489) takes less than a minute so you could
binary-search manually), or enter "nth_prime(10^10)" into Sage
([https://www.sagemath.org/](https://www.sagemath.org/)) to get the result
pretty much instantly (after it starts up, which takes a while), or....

------
gregfjohnson
The stated moral of the story (last line of the article): "Brains win again
over brawn: a well-designed, mathematically knowledgeable algorithm beats
brute force!"

This reminds me of a claim I heard from a numerical analysis colleague:
"Algorithms of today running on hardware from the 1950's would beat algorithms
of the 1950's running on hardware of today."

It was a thought-provoking comment. It would be less hyperbolic if prefaced
with "There exist some problems of practical significance for which.."

The question is, what might be some examples? What are some problems for which
the time or space complexity of the best known solution has decreased over the
last several decades to the point that the above statement would be true of
them?

~~~
kens
The Fast Fourier Transform (1965) is one very important algorithm that comes
to mind, speeding up Fourier Transforms from O(N^2) to O(N log N). Quicksort
(published 1961) might be another example, but since sorts in the 1950s
usually ran on tape drives, it's hard to compare.

On the whole, computers of the 1950s were extremely memory-limited, so many
modern algorithms aren't entirely relevant. For example, the IBM 7090 (1959)
was a large-scale scientific computer and had an address space of 32K words.
As for performance, it did 39,500 multiplications per second. A modern
supercomputer is trillions of times faster; that makes up for a lot of bad
algorithms.

Most modern problems are going to have a hard time fitting in a 1950s
computer, regardless of the algorithm. On the whole, I think you'd be better
off with 1950s algorithms on a modern computer than vice versa. On the other
hand, modern cryptographic algorithms on old hardware would be infinitely
better than old algorithms on modern hardware.

~~~
rstuart4133
Merge sorts are typically used when sorting data on magnetic tapes. Polyphase
merge was popular. All merge sorts are O(N log(N)). They were ideal for tapes
as they read and write data sequentially.

~~~
rsecora
This is why I love HN. I was going to the shelving to check the cost in the
Wirth book [1], and the right number was already published in HN.

[1] Algorithms + Data Structures = Programs / Niklaus Wirth.

------
barrkel
> _Brains win again over brawn: a well-designed, mathematically knowledgeable
> algorithm beats brute force!_

But it didn't, though, did it. Brute force got an answer before brains found
the smarter way.

It's great that the well-designed, math knowledgeable algorithm beats brute
force, but it takes time to design something well, and to gain the knowledge,
if it even exists, which is not a guaranteed prior. In the meantime, your
quickly written brute force approach can be making progress.

IMO always try brute force first if there isn't a fairly obvious alternative.
Surprisingly often, brute force is enough.

~~~
svat
As they say, “A couple of months in the laboratory can frequently save a
couple of hours in the library.” Or in this case, a thousand-plus hours of
computer time saved a few minutes of looking it up
([https://en.wikipedia.org/w/index.php?title=Prime_number&oldi...](https://en.wikipedia.org/w/index.php?title=Prime_number&oldid=958581307#Number_of_primes_below_a_given_bound))
or asking a mathematician.

~~~
rlonn
and was that entry available on Wikipedia in 1994?

~~~
svat
Obviously Wikipedia didn't exist in 1994; I only gave the link for someone who
is actually interested in looking it up today. A paper clearly explaining how
to compute π(x) (I was just reading it) had come out in 1985:
[https://www.ams.org/journals/mcom/1985-44-170/S0025-5718-198...](https://www.ams.org/journals/mcom/1985-44-170/S0025-5718-1985-0777285-5/S0025-5718-1985-0777285-5.pdf)

~~~
barrkel
Where would the average bloke in the street go to find that in 1994? And where
would they get _that_ knowledge?

(I mean, these days, the first thing you'd do is Google, the second thing is
ask on SO or similar. You'd do this before even thinking about coding
something yourself. So the bar to jumping to brute force has shifted
somewhat.)

~~~
gmfawcett
(I'd love to live in a town where the average bloke in the street was
interested in fast algorithms for computing π(x), that would be a neat place!)

You'd seek out a mathematician, or visit a university library and ask for the
maths librarian. If you didn't know about those resources, you'd go to your
public library, and hopefully a librarian would point you in the right
direction. All of these search algorithms still work today. :)

~~~
eggy
As someone who had to write letters for information or ask experts over the
phone in the 70s, 80s, and part of the 90s (BBSs in the late 80s to 90s for
me), you'd have to have good luck finding the person with the bit of
specialized knowledge you sought, and hope they had it. A nostalgic note: Our
sixth grade teacher made us pick four big companies and write letters to them
asking for information about them and their products. I remember being
ecstatic getting letters, pamphlets and even items from them. Toothpaste from
Colgate. The pamphlets were so much more informative than the marketing
material today. They were pitched to a smarter audience possibly purchasing
agents, etc. hTe fastest response was almost three weeks. The rest came a
month and later.

~~~
gmfawcett
I was just a fledgling in the late days of the BBSs, so I didn't spend too
much time there!

Fair points about the trouble of finding the right expert. I also wasn't on
any of the math-related Usenet groups back in the day (I was more of a
comp.lang.* lurker), but I wonder if there was a good mingling of experts and
novices in those groups. These days, you can (e.g.) visit /r/math or
/r/mathematics on Reddit, and you'll occasionally see interesting novice
questions being picked up by PhDs across various math fields -- sometimes with
surprisingly deep responses. It's rare that a novice question merits such
attention, but the fact that it can can happen -- that such a forum exists --
is delightful.

You've also reminded me of something, vaguely -- one of the major US
universities used to have a phone number that you could call and ask them
about nearly anything. They would research the question and get back to you
with an answer if they could. I wish I could remember which university it was,
maybe the number still exists?

------
gerdesj
Hui's immediate response was, "Do you start counting from 0 or 1?"

Counting numbers are defined as the integers starting from one. I'm probably
lacking a bit of rigour there. err ... Define 1 = S or is that S = 1 or S is a
thing or 1 is a thing or something and then put more S (s) in until you run
out of S (essessses) then you have reached infinity. If it's not a really big
infinity then keep adding S (sssssss) or remove a few and add some more.
You'll get there eventually - lots of Ss or infinity, or not, who knows? If
you run out of S then try adding some T. Everything is better with T.

He would have better off with: "Do you prefer green trees or brown trees?"
That would have simply sounded silly, rather than +1 insightful.

~~~
beervirus
Yeah that comment didn’t make much sense. No one would seriously dispute that
2 is the first prime.

~~~
Someone
Because “not prime” isn’t the same as “composite” (1 is neither prime nor
composite in the modern view) it isn’t that simple.

Some ancient mathematicians such as Aristotle and Plato solved that problem by
saying 2 is the first number. Others stated 3 to be the first prime

That isn’t holdable once you accept 0 and negative integers to be numbers.

[https://cs.uwaterloo.ca/journals/JIS/VOL15/Caldwell2/cald6.h...](https://cs.uwaterloo.ca/journals/JIS/VOL15/Caldwell2/cald6.html)
states that, for example, Goldbach thought 1 to be prime at some time (in a
letter to Euler), as did Legendre, Lebesgue (sometimes), Cayley, Kronecker,
Hardy, Lehmer (as the article discussed also says), and the aliens in Carl
Sagan’s “Contact”.

It also gives fairly recent publications that state 1 is a prime.

In the end, whether we consider 1 to be prime is more a choice (just as
mathematicians commonly chose to pick 0⁰ = 1) than that it necessarily the
case. It just is the better choice
([https://en.wikipedia.org/wiki/Prime_number#Primality_of_one](https://en.wikipedia.org/wiki/Prime_number#Primality_of_one))

~~~
beervirus
We’re not talking about whether or not 1 is prime. We’re talking about whether
2 is the _first_ prime or the _zeroth_ prime.

No one would seriously say the latter.

~~~
gerdesj
Can there be a zeroth prime? We are counting things and I think that counting
starts at one.

Given that a counting always starts at one, because that is what defines
counting, then there cannot be a zeroth prime.

~~~
simiones
Well, technically, rigorously defined counting starts at 0, not at 1.

You can define counting as defining an injective function from your set to the
natural numbers, and then you need to have some element going to 0 - as per
the definition of a countable set[0].

Also, both the cardinal[1] and the ordinal[2] numbers are defined as starting
from 0, just like the cardinal numbers.

[0]
[https://en.wikipedia.org/wiki/Countable_set](https://en.wikipedia.org/wiki/Countable_set)

[1]
[https://en.wikipedia.org/wiki/Cardinal_number](https://en.wikipedia.org/wiki/Cardinal_number)

[2]
[https://en.wikipedia.org/wiki/Ordinal_number](https://en.wikipedia.org/wiki/Ordinal_number)

~~~
gerdesj
"Countable" is not the same as the "countable numbers" nor is it the same as
the process of "counting".

The process of counting might be defined as what starts to happen when you
stick up one finger and say something to emphasise what that finger means.
That something will not be zero. Ever.

When you count your sheep into your pen, you will of course start: "Yan, tan,
tither, toe" Trust me that yan does not mean zero.

Also note that if you search those terms, you will get a valid result and
conclude I've misspelt some of those Cumbric words. I haven't, according to
living relos of mine. Speling is a bit odd anyway when you go back a few
centuries and I'll wager that tither is more likely than tethera because it is
very slightly more easy to say. Tethera is three syllables but tether is two,
bordering on one. However tethera could be pronounced "tethra", ie drop the
extra e when spoken.

The counting numbers are fairly rigorously defined and are the numbers we use
when we don't have access to more than the usual four dimensions, imaginary
thingies, quaternions, etc etc.

The counting numbers start at one (probably)

------
nautilus12
Brains may win over brawn, but the problem is that brains don't generalize. It
requires bespoke creative mathematical work. In that sense, the brute force
method while slower has an advantage in that it could theoretically be derived
more easily (from another algorithm for example or by a general intelligence
system). Direct analytical results are always faster and more accurate than
numerical results, but numerical results are more reliably attained.

~~~
chenglou
Turns out, we end up evolving said general intelligence system into doing
bespoke creative work, and it slows down the compilation time by around as
much as it takes a human to think up the analytical solution =P

Then we end up with a tunable knob to speed up compilation. But then this more
or less reduces to coding up either method depending on the situation, aka
what we began with!

~~~
nautilus12
Valid point! I think you are right about this

------
dvirsky
> Even a Boolean vector taking just 1 bit per element would have to be more
> than 3e10 bytes long, so it was clear that the problem had to be
> partitioned.

That's 28GB. That's just astronomical in those days' terms, even stored on
hard drives it would have taken a massive array of commodity HDDs. I love
these little reminders of how far we've come.

~~~
stabbles
There is a very beautiful way to do it using much less memory.

You need O(sqrt(n)/log(n)) memory, not O(n), if you keep track of the list of
primes found so far, and a fixed-size interval over which you sieve: [1, k],
[k+1, 2k], [2k+1, 3k], ...

Now, for the beautiful part, if you skip multiples of 2, 3 and 5, and work in
blocks of 2 * 3 * 5 = 30 numbers, you only need to sieve {1, 7, 11, 13, 17,
19, 23, 29} + i*30. 8 booleans, that happen to fit in 1 byte. So 1 byte = 1
interval of 30 numbers.

Next, you can unroll the sieving loop, but no compiler will do this for you,
you have to do it by hand. This allows you to sieve up to 8 multiples per
iteration.

I have an implementation of it in Julia here:
[https://github.com/haampie/FastPrimeSieve.jl](https://github.com/haampie/FastPrimeSieve.jl)
which was influenced by
[https://github.com/kimwalisch/primesieve](https://github.com/kimwalisch/primesieve).

~~~
ctchocula
This solution is beautiful. Thank you for sharing. No longer will I need to
dynamically allocate huge arrays when solving prime number-related problems on
Project Euler.

------
JoeAltmaier
Concrete proof there is no largest prime number: color the number line
starting at zero, blackening every other number, every third, every fifth and
so on up to every 'theoretical-biggest-prime'. Calculate PIPrime, the product
of all of the prime numbers. All of these blackening sequences land on that
number (its a multiple of all of them).

Note that the blackened-integer sequence is symmetrical from zero to PIPrime -
same forward and backward. In fact it starts to repeat itself from there.

Now note that '1' was not blackened. So of course, the number to the right or
left of PIPrime is not blackened. So that number must also be prime (not a
multiple of any known prime) or there is some other prime number, larger than
the known largest, that would have landed there. In either case, there is a
larger prime than you knew.

~~~
JoeAltmaier
i.e. 'concrete mathematical' proof, a proof constructed from physical
references rather than algebraic manipulation

------
lmilcin
455,052,512 is obviously not a prime so this must be an error in the article.

~~~
quietbritishjim
The article claims that to be the number of primes less than 10e10, not that
it is a prime itself.

~~~
lmilcin
Oops. You are right.

~~~
smitty1e
Let us show Mersenne toward all, and be Stern with none.

------
chrismsimpson
Let's not an say we did

------
blackrock
Remind me again, what are some of the benefits to finding ever larger prime
numbers?

Not that there is anything wrong with doing so.

Was it to enhance public key encryption possibilities?

~~~
xelxebar
It's more about the techniques developed along the way than the actual number
itself.

What's the point of running a marathon? Is it to expand your hunting range?

~~~
bakul
Some times it is just curiosity. Our "monkey mind" can't resist such
challenges!

