Factors of an integer: Difference between revisions
Content added Content deleted
m (J: explain what was called "simple" because mechanical simplicity does not mean people will automatically understand it) |
ReeceGoding (talk | contribs) (→{{header|R}}: Filter solution) |
||
Line 4,444: | Line 4,444: | ||
=={{header|R}}== |
=={{header|R}}== |
||
===Array solution=== |
|||
<lang R>factors <- function(n) |
<lang R>factors <- function(n) |
||
{ |
{ |
||
Line 4,454: | Line 4,455: | ||
one.to.n[(n %% one.to.n) == 0] |
one.to.n[(n %% one.to.n) == 0] |
||
} |
} |
||
}</lang> |
|||
} |
|||
{{out}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
<pre> |
<pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
[[1]] |
[[1]] |
||
[1] 1 3 5 9 15 45 |
[1] 1 3 5 9 15 45 |
||
Line 4,466: | Line 4,468: | ||
[1] 1 2 4 8 16 32 64 |
[1] 1 2 4 8 16 32 64 |
||
</pre> |
</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}}== |
=={{header|Racket}}== |