Sequence: smallest number with exactly n divisors: Difference between revisions
Content added Content deleted
(→{{header|jq}}: streaming) |
ReeceGoding (talk | contribs) (Added R. So short that I'm wondering if I've missed a point.) |
||
Line 1,416: | Line 1,416: | ||
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre> |
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre> |
||
=={{header|R}}== |
|||
Could probably be speeded up by caching the results of divisorCount, but it's quick enough already. Not to mention, delightfully short. |
|||
<lang R>#Need to add 1 to account for skipping n. Not the most efficient way to count divisors, but quite clear. |
|||
divisorCount <- function(n) length(Filter(function(x) n %% x == 0, seq_len(n %/% 2))) + 1 |
|||
smallestWithNDivisors <- function(n) |
|||
{ |
|||
i <- 1 |
|||
while(divisorCount(i) != n) i <- i + 1 |
|||
i |
|||
} |
|||
print(sapply(1:15, smallestWithNDivisors))</lang> |
|||
{{out}} |
|||
<pre>[1] 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |