[R] Function to find prime numbers

Thomas Lumley tlumley at u.washington.edu
Tue Oct 13 15:41:34 CEST 2009


On Tue, 13 Oct 2009, AJ83 wrote:

>
> I need to create a function to find all the prime numbers in an array. Can
> anyone point me in the right direction?

It depends a bit on how big the numbers are.  If the array is large but the numbers are not very large the 
fastest approach is probably to create a vector of small primes and use
    my_array %in% smallprimes.

For example, the first 1000 primes are at http://primes.utm.edu/lists/small/1000.txt
and the first 10000 are on the same site.


       -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list