Just an example of what I am looking for: I could represent every odd number with a bit e.g. for the given range of numbers (1, 10], starts at 3:
1110
The following dictionary can be squeezed more right? I could eleminate multiples of five with some work, but numbers that end with 1, 3, 7 or 9 must be there in the array of bits. Hope this would clarify what I want.
I am looking for the best algorithm, to check if a number is prime i.e. a boolean function:
bool isprime(number);
I would like to know the best algorithm to implement this functionality. Naturally, there would be a data structure I could query. I define the best algorithm, to be the algorithm that produces a data structure with lowest memory consumption for the range (1, N], where N is a constant.
To find if the number or numbers in a range is/are prime.
The fastest algorithm for general prime testing is AKS. The Wikipedia article describes it at lengths and links to the original paper.
If you want to find big numbers, look into primes that have special forms like Mersenne primes.
The algorithm I usually implement (easy to understand and code) is as follows (in Python):
It's a variant of the classic
O(sqrt(N))
algorithm. It uses the fact that a prime (except 2 and 3) is of form6k - 1
or6k + 1
and looks only at divisors of this form.Sometimes, If I really want speed and the range is limited, I implement a pseudo-prime test based on Fermat's little theorem. If I really want more speed (i.e. avoid O(sqrt(N)) algorithm altogether), I precompute the false positives (see Carmichael numbers) and do a binary search. This is by far the fastest test I've ever implemented, the only drawback is that the range is limited.
https://en.wikipedia.org/wiki/AKS_primality_test
Python 3:
I compared the efficiency of the most popular suggestions to determine if a number is prime. I used
python 3.6
onubuntu 17.10
; I tested with numbers up to 100.000 (you can test with bigger numbers using my code below).This first plot compares the functions (which are explained further down in my answer), showing that the last functions do not grow as fast as the first one when increasing the numbers.
And in the second plot we can see that in case of prime numbers the time grows steadily, but non-prime numbers do not grow so fast in time (because most of them can be eliminated early on).
Here are the functions I used:
this answer and this answer suggested a construct using
all()
:This answer used some kind of while loop:
This answer included a version with a
for
loop:And I mixed a few ideas from the other answers into a new one:
Here is my script to compare the variants:
Running the function
compare_functions(n=10**5)
(numbers up to 100.000) I get this output:Then, running the function
compare_functions(n=10**6)
(numbers up to 1.000.000) I get this output:I used the following script to plot the results:
Smallest memory? This isn't smallest, but is a step in the right direction.
Of course, you have to specify the definition of
CheckPrimality
.