
Dijkstra's Prime Number Algorithm - bpierre
http://heinrichhartmann.com/2016/04/03/Dijkstra%27s-Prime-Number-Algorithm.html
======
amelius
> What is remarkable about this algorithm is, that it uses no divisions at
> all!

The same holds for the Sieve of Eratosthenes [1]

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

~~~
heinrichhartman
Author here: Yes, that true, thanks for pointing it out. [In fact,
Erathosthenes is mentioned later in that note.]

I still find it remarkable, that you can avoid divisions, while not to
computing all multiples up-front and marking the results in a large table.

~~~
davidamarquis
The algorithm is very nice. But I think you should think about what it means
to divide one integer by another. The quotient of that operation is an
integer. The remainder is an integer.

Can you express the number in terms of the quotient and remainder?

Using this expression write an algorithm that outputs the quotient and
remainder using only addition, multiplication and checking equality with
integers.

I think doing this would make the algorithm seem less surprising.

------
sajid
There's a simple combinatorial proof of the existence of a prime between n and
n^2:

[https://www.quora.com/Is-there-a-prime-between-every-
natural...](https://www.quora.com/Is-there-a-prime-between-every-natural-
number-greater-than-1-and-its-square)

------
goldenkey
The gaps between primes are easily produced using two operations on a list of
numbers, mirroring and partitioning.

Each prime generates a spacing sequence to find future primes, each prime is
effectively casting a new periodic wave function. 2 produces a wave that hits
all even numbers. 3 produces a wave that hits every 6 numbers (2 already gets
everything else.) 5 does every {20,10}, etc...

You can see the process of generating the sieve waveform, or more technically,
the prime gaps, here:

[http://math.stackexchange.com/questions/311610/modified-
eule...](http://math.stackexchange.com/questions/311610/modified-eulers-
totient-function-for-counting-constellations-in-reduced-
residue/1055504#1055504)

Look at the 2nd answer, it explains the process. Remember, all prime numbers
do, is cast a waveform onto the existing sieve waveform. Just chaotic
interference. Division as an immediate reaction to primes is moreso because
primes are taught pretty poorly. As mystic nuggets. Rather than chaotic
interference.

~~~
theoh
You've posted about this "waveform" idea before, and to be honest it seems
like you are making a big deal out of the obvious fact that each newly found
prime eliminates some possible greater numbers from the search by being a
factor.

With friendly intentions, I have to tell you that you risk sounding like a
math crank.

~~~
goldenkey
The waveform idea is simply to convey the sieve to non math folk in a more
intriguing way. I dont see how talking about first differences of reduced
residue sets of primorials is crank. Its being studied as we speak by PHD Fred
Holt from UWashington...

Wheres the issue with the method of mirroring and partitioning I discussed? Do
you have something better than wont involve division?

Also, there is a lot of merit in understanding how the sieve works because gap
sequences work until the next prime squared. As the primes tend toward
infinity, the sieve matches all primes. Its a valid way to study primes aside
from the attacks on the Zeta function

~~~
theoh
I am open to being convinced that you have new insights, but the reduced
residue sets idea is not a magic bullet. If it was a magic bullet we would be
having a different conversation.

Basically I am encouraging you to discuss this in a way or in a context in
which people will engage more readily with your ideas, either to accept or
reject them. HN is not full of number theorists.

Edit: This is a case where textual communication is really inadequate to
determine the seriousness and, dare I say it, credibility of an interlocutor.

~~~
goldenkey
You can see Fred's papers here:
[http://arxiv.org/find/math/1/au:+Holt_F/0/1/0/all/0/1](http://arxiv.org/find/math/1/au:+Holt_F/0/1/0/all/0/1)

You misunderstand me. I don't claim that the dRRS is a magic bullet. But it
certainly isn't crank..

------
cyphar
The coolest technique I know of for checking primes is a variant of the Lucas
sequence method (which is what GIMPS uses to find Mersenne primes)[1].
Essentially, you generate terms in a sequence in a constant-memory fashion
that doesn't require you to know the factors, using just a modulo in a
generating function.

[1]:
[https://www.youtube.com/watch?v=lEvXcTYqtKU](https://www.youtube.com/watch?v=lEvXcTYqtKU)

------
kmill
This algorithm seems similar to one I found* which is a Sieve of Eratosthenes
that doesn't require deciding how big of an array to allocate at the
beginning. Essentially, you reverse the order of the two main loops, modifying
the main data structure accordingly.

[http://www.kylem.net/stuff/sieve_eratosthenes.html](http://www.kylem.net/stuff/sieve_eratosthenes.html)

I don't completely understand it yet, but I think the Dijkstra version might
have optimized the structure which contains the multiples of known primes,
where I just put it into a dictionary.

* I initially saw it in a paper about how the Sieve is generally implemented incorrectly in Haskell, but I made a small Python generator implementation.

------
mrcactu5
A long and detailed list of prime number structures in Haskell

[https://wiki.haskell.org/Prime_numbers](https://wiki.haskell.org/Prime_numbers)

And a long list of algorithms. Many of which are surprising and probably not
that efficient. The first few are:

[https://wiki.haskell.org/Prime_numbers_miscellaneous#Prime_W...](https://wiki.haskell.org/Prime_numbers_miscellaneous#Prime_Wheels)

Efficiency is kind of a relative issue, since you don't know the job
beforehand or which one would be optimal

~~~
tromp
There's also this obfuscated

    
    
        let f='.';o c(x:y)=x:c y;z c(x:y)=f:c y;p n='p':ap fix p(o.n)in f:f:p z
    

defining the `bitmap' of prime numbers

    
    
        ..pp.p.p...p.p...p.p...p.....p.p.....p...p.p...p..  ...
    

while avoiding arithmetic altogether.

------
jebediah
This is slower than the sieve right?

~~~
heinrichhartman
I think so, but the memory usage is drastically lower: Only O(x/ln(x)) vs.
O(x), for getting all primes up to x.

~~~
ableal
(edit: typo gone, thanks for the interesting write-up)

~~~
heinrichhartman
Indeed! Thanks a lot. Its corrected now.

... no wait: It's corrected now. ;)

------
oluckyman
Given its author, it would be even more remarkable if this algorithm was
incorrect. See my comment on the article.

~~~
oluckyman
The algorithm is indeed incorrect, as I've explained in a more recent comment
on the article.

