Saturday, May 23, 2009

Prime Numbers

Once upon a time I wrote some functions for finding and checking prime numbers. In this library of functions you'll find the following:


  • getPrimes() - finds prime numbers up to a specified limit
  • isPrime() - checks if a number is prime using modular division against odd numbers
  • isPrime2() - checks if a number is prime using modular division against known primes
  • primeCount() - counts the number of primes less than or equal to the specified number
  • isComposite() - the opposite of prime
  • isPrimeSpeedTest() - races isPrime againts isPrime2

In theory, checking against known primes should be faster than checking against all odd numbers, but it turned out to be slower because I was first finding the primes with getPrimes(). If a list of prime numbers was hardcoded in an array it might be faster for smaller numbers, and then the odd number method could be used for larger numbers.


View ASP implementation on Snipplr

No comments: