Factors of an integer: Difference between revisions

→‎{{header|R}}: Filter solution
m (J: explain what was called "simple" because mechanical simplicity does not mean people will automatically understand it)
(→‎{{header|R}}: Filter solution)
Line 4,444:
 
=={{header|R}}==
===Array solution===
<lang R>factors <- function(n)
{
Line 4,454 ⟶ 4,455:
one.to.n[(n %% one.to.n) == 0]
}
}</lang>
}
{{out}}
factors(60)</lang>
1 2 3 4 5 6 10 12 15 20 30 60
<lang R>factors(c(45, 53, 64))</lang>
<pre>
>factors(60)</lang>
[1] 1 2 3 4 5 6 10 12 15 20 30 60
<lang R>factors(c(45, 53, 64))</lang>
[[1]]
[1] 1 3 5 9 15 45
Line 4,466 ⟶ 4,468:
[1] 1 2 4 8 16 32 64
</pre>
===Filter solution===
With identical output, a more idiomatic way is to use R's Filter and Vectorize:
<lang R>factors<-function(n){Filter(function(x) n %% x == 0, x=c(1:(n%/%2),n))}
 
#If you want to use a vector of integers as an input, as in the previous solution, use:
manyFactors<-function(vec){Vectorize(factors)(vec)}</lang>
 
=={{header|Racket}}==
331

edits