Proper divisors: Difference between revisions

m
→‎Filter solution: Clarification
m (→‎Filter solution: Missed treatment of 1 as a special case.)
m (→‎Filter solution: Clarification)
Line 4,316:
<lang r>#Task 1
#Has no input error checking.
divisorsproperDivisors<-function(n)
{
if(n==1)(return(NULL))#This seems like bad code, but task 2 demands some output for n=1, which has no proper divisors.
else(Filter(function(x) n %% x == 0, 1:(n%/%2)))
}
Line 4,324:
#Task 2
#The output could be put in to a cleaner form than a list, but this is the idiomatic way.
Vectorize(divisorsproperDivisors)(1:10)
 
#Task 3
Line 4,333:
mostProperDivisors<-function(N)
{
divisorList<-Vectorize(divisorsproperDivisors)(1:N)
numberWithMostDivisors<-which.max(lengths(divisorList))
return(paste0("The number with the most proper divisors between 1 and ",N,
Line 4,343:
{{Output}}
<pre>#Task 2
> Vectorize(divisorsproperDivisors)(1:10)
[[1]]
NULL
331

edits