Algorithm for determining own divisors

I am interested in finding numbers that have the property of having the sum of their own divisors equal to a number. The first example is 6, where the proper divisors are 1 + 2 + 3 = 6.

I wrote the following code in R, but I feel that it is quite inefficient and can be greatly improved.

propDivisor <- function(
    max
)
{
    n<-{}
    for(j in 2:max){
        m<-{}
        for(i in 1:(j/2+1)){
            if(j%%i==0){m<-c(m,i)}
        }   
        if(sum(m)==j){n<-c(n,j)}
    }
return(cat("The proper divisors between 1 and", max, "are", n, ".", sep=" ")    )
}

Does anyone have any suggestions for improving the following code? I believe that here you need to use one of the functions of the application. Maybe it will be a decent golf game for the future?

And, as I know, this is often found here, this is NOT a homework problem, but something like a colleague who today represented an interesting coding rival.

UPDATE:

, . , sapply:

D <- function(n) sum((1:(n-1))[n%%1:(n-1)==0])==n
(2:9000)[sapply(2:9000,D)]
+5
3

, , ( ).

, . .

, , :

  • sqrt (max)
  • , divisor i, max/i , max/i == i, .
+6

2 ^ (n-1) * (2 ^ n-1) , 2 ^ n-1

+2
#include<stdio.h>
#include<math.h>
int main()
{
        int t;
        scanf("%d",&t);
        while(t--)
        {
                long long int n,i,sum= -n;
                scanf("%lld",&n);
                for(i=1;i<=sqrt(n);i++)
                {
                        if(n%i==0)
                        sum = sum + i + n/i;
                }
                printf("%lld\n",sum);
        }
        return 0;
}

~

0

All Articles