Re: [R] Function to find prime numbers

2009-10-13 Thread Romain Francois

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.
AJ


There 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   1FALSE FALSE
2   2 TRUE  TRUE
3   3 TRUE  TRUE
4   4FALSE FALSE
5   5 TRUE  TRUE
6   6FALSE FALSE
7   7 TRUE  TRUE
8   8FALSE FALSE
9   9FALSE FALSE
10 10FALSE 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 #5
|- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc
`- http://tr.im/yw8E : New R package : sos

__
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.


Re: [R] Function to find prime numbers

2009-10-13 Thread Gustaf Rydevik
library(gmp)
?isprime


/Gustaf



On Tue, Oct 13, 2009 at 9:59 AM, AJ83 aljense...@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]]

__
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.


Re: [R] Function to find prime numbers

2009-10-13 Thread Thomas Lumley

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 1 are on the same site.


  -thomas

Thomas Lumley   Assoc. Professor, Biostatistics
tlum...@u.washington.eduUniversity of Washington, Seattle

__
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.


Re: [R] Function to find prime numbers

2009-10-13 Thread Barry Rowlingson
On Tue, Oct 13, 2009 at 2:41 PM, Thomas Lumley tlum...@u.washington.edu wrote:
 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?

 This almost sounds like a homework problem to me... So here's a
solution that you can happily present to a tutor - if you can explain
how it works, then you deserve full marks!

primer=function(v){
  
return(regexpr(^1$|^(11+?)\\1+$,unlist(lapply(v,function(z){paste(rep(1,z),sep='',collapse='')})),perl=TRUE)
== -1)
}

Test:

  (1:30)[primer(1:30)]
 [1]  2  3  5  7 11 13 17 19 23 29

I'm not sure how big a number this works for

R golf anyone?

Barry

__
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.


Re: [R] Function to find prime numbers

2009-10-13 Thread joris meys
On Tue, Oct 13, 2009 at 5:30 PM, Barry Rowlingson
b.rowling...@lancaster.ac.uk wrote:
 On Tue, Oct 13, 2009 at 2:41 PM, Thomas Lumley tlum...@u.washington.edu 
 wrote:
 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?

  This almost sounds like a homework problem to me... So here's a
 solution that you can happily present to a tutor - if you can explain
 how it works, then you deserve full marks!

This almost sounds like an extra assignment to me :-)


 primer=function(v){
  return(regexpr(^1$|^(11+?)\\1+$,unlist(lapply(v,function(z){paste(rep(1,z),sep='',collapse='')})),perl=TRUE)
 == -1)
 }

 Test:

   (1:30)[primer(1:30)]
  [1]  2  3  5  7 11 13 17 19 23 29

 I'm not sure how big a number this works for

 R golf anyone?

I take the challenge and give AJ some more stuff to think about.

First the obvious :

primer=function(v){
 
return(regexpr(^1$|^(11+?)\\1+$,sapply(v,function(z){paste(rep(1,z),sep='',collapse='')}),perl=TRUE)
== -1)
}

Test :
 (1:30)[primer(1:30)]
 [1]  2  3  5  7 11 13 17 19 23 29

Shorter, if I keep the same use of the function primer :

primer=function(v){
 return(sapply(v,function(z){sum(z/1:z==z%/%1:z)})==2)
}
(1:30)[primer(1:30)]
 [1]  2  3  5  7 11 13 17 19 23 29

nchar(primer=function(v){  . . .  (1:30)[primer(1:30)])
[1] 97


This works for all numbers. It also allows for a function primes :

primes=function(v){
return(v[sapply(v,function(z){sum(z/1:z==z%/%1:z)==2})])
}
primes(1:30)
 [1]  2  3  5  7 11 13 17 19 23 29

 nchar(primes=function(v){  . . .  primes(1:30))
[1] 91

Cheers
Joris

__
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.