21.10.2011, 10:36
Quote:
http://y-less.pastebin.ca/2092126
Fast prime number check. This implementation is specifically tuned to 32bit numbers (i.e. what SA:MP PAWN uses). The function contains a list of all the prime numbers below 65536. Although that seems like a lot, remember that this is less than 0.002% of all the 32bit prime numbers! However, the number 65536 is important here as 65536*65536=4294967296, which is exactly the number of states in 32bits. Why is this significant? Because when checking for prime numbers, you only need to see if it is a product of any number UP TO ITS SQUARE ROOT. What's more, if a number is not a product of 3, it will not be a product of 6 or 9 so we only need to check if prime numbers are factors. If we have a list of all the prime numbers up to the square root of any number we could possibly check, this will all go much faster! Note that if the number you are checking is less than 65536 the code will simply run a binary search (with an approximated initial upper bound for speed) to see if it is in the array. |