Factors of an integer: Difference between revisions

→‎Filter solution: Removed bug.
(Undo revision 306600 by ReeceGoding (talk))
(→‎Filter solution: Removed bug.)
Line 4,470:
===Filter solution===
With identical output, a more idiomatic way is to use R's Filter and Vectorize:
<lang R>
<lang R>factors<-function(n){Filter(function(x) n %% x == 0, x=c(1:(n%/%2),n))}
factors<-function(n)
{
<lang R>factors allButN<-function(n){Filter(function(x) n %% x == 0, x=c(1:(n%/%2),n))}
union(allButN,n)#We use union rather than the c() to prevent factors(1) from returning 1 twice.
}
 
#If you want to use a vector of integers as an input, as in the previous solution, use:
331

edits