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.
Similar idea to the AKS algorithm which has been mentioned
I think one of the fastest is my method that I made.
Way too late to the party, but hope this helps. This is relevant if you are looking for big primes:
To test large odd numbers you need to use the Fermat-test and/or Miller-Rabin test.
These tests use modular exponentiation which is quite expensive, for
n
bits exponentiation you need at leastn
big int multiplication andn
big int divison. Which means the complexity of modular exponentiation is O(n³).So before using the big guns, you need to do quite a few trial divisions. But don't do it naively, there is a way to do them fast. First multiply as many primes together as many fits into a the words you use for the big integers. If you use 32 bit words, multiply 3*5*7*11*13*17*19*23*29=3234846615 and compute the greatest common divisor with the number you test using the Euclidean algorithm. After the first step the number is reduced below the word size and continue the algorithm without performing complete big integer divisions. If the GCD != 1, that means one of the primes you multiplied together divides the number, so you have a proof it's not prime. Then continue with 31*37*41*43*47 = 95041567, and so on.
Once you tested several hundred (or thousand) primes this way, you can do 40 rounds of Miller-Rabin test to confirm the number is prime, after 40 rounds you can be certain the number is prime there is only 2^-80 chance it's not (it's more likely your hardware malfunctions...).
Most of previous answers are correct but here is one more way to test to see a number is prime number. As refresher, prime numbers are whole number greater than 1 whose only factors are 1 and itself.(source)
Solution:
Typically you can build a loop and start testing your number to see if it's divisible by 1,2,3 ...up to the number you are testing ...etc but to reduce the time to check, you can divide your number by half of the value of your number because a number cannot be exactly divisible by anything above half of it's value. Example if you want to see 100 is a prime number you can loop through up to 50.
Actual code:
With help of Java-8 streams and lambdas, it can be implemented like this in just few lines:
Performance should be close to O(sqrt(N)). Maybe someone find it useful.
I have got a prime function which works until (2^61)-1 Here:
Explanation:
The
all()
function can be redefined to this:The
all()
function just goes through a series of bools / numbers and returnsFalse
if it sees 0 orFalse
.The
sqrt()
function is just doing the square root of a number.For example:
The
num % x
part returns the remainder of num / x.Finally,
range(2, int(sqrt(num)))
means that it will create a list that starts at 2 and ends atint(sqrt(num)+1)
For more information about range, have a look at this website!
The
num > 1
part is just checking if the variablenum
is larger than 1, becuase 1 and 0 are not considered prime numbers.I hope this helped :)