Factors of an integer: Difference between revisions
Content added Content deleted
(Moved Polyglot:PL/I and PL/M into the correct place) |
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
||
Line 5,379: | Line 5,379: | ||
=={{header|R}}== |
=={{header|R}}== |
||
===Array solution=== |
===Array solution=== |
||
<lang |
<lang rsplus>factors <- function(n) |
||
{ |
{ |
||
if(length(n) > 1) |
if(length(n) > 1) |
||
Line 5,404: | Line 5,404: | ||
===Filter solution=== |
===Filter solution=== |
||
With identical output, a more idiomatic way is to use R's Filter. |
With identical output, a more idiomatic way is to use R's Filter. |
||
<lang |
<lang rsplus>factors <- function(n) c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n) |
||
#Vectorize is an interesting alternative to the previous solution's lapply. |
#Vectorize is an interesting alternative to the previous solution's lapply. |
||
manyFactors <- function(vec) Vectorize(factors)(vec)</lang> |
manyFactors <- function(vec) Vectorize(factors)(vec)</lang> |