By KlausG
via tellingmachine.com
Published: Feb 22 2010 / 22:56
n PHP is really no ideal way to test large integers and determine whether they are prime numbers or not. The most popular algorithm for finding prime numbers is a memory and resource hog. It is called The Sieve of Eratosthenes.
Besides the Sieve I also implemented a prime number test by initializing an array of prime numbers from a file that contains the first 100,000 prime numbers. This is of course much faster, but would require files that contain all prime numbers up to a specific large number. The files of course could be partitioned, which would also increase performance.
Comments
yakkoh replied ago:
Faster for evenNumber is $number & 1
where & is the bit and.
see http://www.php.net/manual/en/language.operators.bitwise.php
also available for IsInteger: is_numeric and is_int.
Voters For This Link (9)
Voters Against This Link (0)