[S] Prime divisors

Michael Bramley, M.Sc. (lifer@fuse.net)
Mon, 14 Sep 1998 20:50:51 -0500


Ok, I'll bite. How about the following to return only the prime divisors of
an integer n?
Note I'm using Frank Harrell's improved factorize(), and I included the
loop to see if
anyone can do better. C'mon, what do you expect on such short notice?

divisors<-function(n)
{
div <- factorize(n)
idx <- rep(F, length(div))
for(i in 2:length(div))
if(length(factorize(div[i])) == 2) idx[i] <- T
div[idx]
}

Michael Bramley

----------
> From: Frank E Harrell Jr <fharrell@virginia.edu>
> To: s-news <s-news@wubios.wustl.edu>
> Subject: [S] An improved factorize()
> Date: Saturday, September 12, 1998 9:34 PM
>
> Here is a modification of Michael Bramley's (bramley.m@pg.com) factorize
> function with memory usage of approx. the square root of the original.
>
> factorize <- function(n)
> {
> p <- n/(z <- 1:ceiling(sqrt(n)))
> z <- z[trunc(p) == p]
> unique(c(z, rev(n/z)))
> }
>
>
---------------------------------------------------------------------------
> Frank E Harrell Jr
> Professor of Biostatistics and Statistics
> Director, Division of Biostatistics and Epidemiology
> Dept of Health Evaluation Sciences
> University of Virginia School of Medicine
> http://hesweb1.med.virginia.edu/biostatistics.html
>
>
> -----------------------------------------------------------------------
> This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
> send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
> message: unsubscribe s-news
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news