I need to create a function to find all the prime numbers in an array. Can anyone point me in the right direction? Thank you. AJ -- View this message in context: http://www.nabble.com/Function-to-find-prime-numbers-tp25868633p25868633.html Sent from the R help mailing list archive at Nabble.com.
library(gmp) ?isprime /Gustaf On Tue, Oct 13, 2009 at 9:59 AM, AJ83 <aljensen01@gmail.com> wrote:> > I need to create a function to find all the prime numbers in an array. Can > anyone point me in the right direction? > Thank you. > AJ > -- > View this message in context: > http://www.nabble.com/Function-to-find-prime-numbers-tp25868633p25868633.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik [[alternative HTML version deleted]]
On 10/13/2009 09:59 AM, 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? > Thank you. > AJThere is isprime in package gmp. > data.frame( number = 1:10, is.prime = isprime( 1:10 ) > 1, is.probably.prime = isprime(1:10) > 0 ) number is.prime is.probably.prime 1 1 FALSE FALSE 2 2 TRUE TRUE 3 3 TRUE TRUE 4 4 FALSE FALSE 5 5 TRUE TRUE 6 6 FALSE FALSE 7 7 TRUE TRUE 8 8 FALSE FALSE 9 9 FALSE FALSE 10 10 FALSE FALSE See this: http://jostamon.blogspot.com/2009/02/goldbachs-comet.html for an example of its use. Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/BcPw : celebrating R commit #50000 |- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc `- http://tr.im/yw8E : New R package : sos
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