# primefactors: a simple recursive function to return the prime factors of # a positive integer n. By Jeffrey Rosenthal (www.probability.ca). primefactors = function(n) { if ( (!is.double(n)) || (n%%1 != 0) || (n<=0) ) { cat("Error: n must be a positive integer.\n") return(NULL) } else if (n==1) { return(1) } else { # Find the next prime factor. for (p in 2:sqrt(n)) { if (p==n) return(p) if (n %% p == 0) return( c(p, primefactors(n/p)) ) } return(n) } }