Proper divisors: Difference between revisions

→‎{{header|R}}: New solution.
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(→‎{{header|R}}: New solution.)
Line 4,300:
 
=={{header|R}}==
===Package solution===
 
{{Works with|R|3.3.2 and above}}
 
Line 4,316:
*** max number of divisors: 79
*** for the following indices: 15120 18480
</pre>
 
===Filter solution===
<lang r>
#Task 1
#Has no input error checking.
divisors<-function(n)
{
Filter(function(x) n %% x == 0, 1:(n%/%2))
}
 
#Task 2
#The output could be put in to a cleaner form than a list, but this is the idiomatic way.
Vectorize(divisors)(1:10)
 
#Task 3
#Although there are two, the task only asks for one suitable number so that is all we give.
#Similarly, we have seen no need to make sure that "divisors" is only a plural when it should be.
mostProperDivisors<-function(N)
{
divisorList<-Vectorize(divisors)(1:N)
numberWithMostDivisors<-which.max(lengths(divisorList))
return(paste0("The number with the most divisors between 1 and ",N,
" is ",numberWithMostDivisors,
". It has ",length(divisorList[[numberWithMostDivisors]])," divisors."))
}
mostProperDivisors(20000)
</lang>
 
{{Output}}
 
<pre>
#Task 2
> Vectorize(divisors)(1:10)
[[1]]
[1] 1
 
[[2]]
[1] 1
 
[[3]]
[1] 1
 
[[4]]
[1] 1 2
 
[[5]]
[1] 1
 
[[6]]
[1] 1 2 3
 
[[7]]
[1] 1
 
[[8]]
[1] 1 2 4
 
[[9]]
[1] 1 3
 
[[10]]
[1] 1 2 5
 
#Task 3
> mostProperDivisors(20000)
[1] "The number with the most divisors between 1 and 20000 is 15120. It has 79 divisors."
 
</pre>
 
331

edits