C, C++, C#, D, Java, 65 62 bytes
int d(int n){int s=0,i=1;for(;i<=n;++i)s+=n%i>0?0:i;return s;}
This works in all theses 5 programming languages because of similarities.
C, C++ and D optimization : 62 60 bytes
In C++ and D, integers convert implicitly to booleans ( Zero => false, Not Zero => true ), so you don't need to have the !=0
int d(int n){int s=0,i=1;for(;i<=n;++i)s+=n%i?0:i;return s;}
D optimization : golfy template system, 55 bytes
T d(T)(T n){T s,i=1;for(;i<=n;++i)s+=n%i?0:i;return s;}
Code to test :
C :
printf("%d %d %d %d %d", d(7), d(15), d(20), d(1), d(5));
C++ :
std::cout << d(7) << ' ' << d(15) << ' ' << d(20) << ' ' << d(1) << ' ' << d(5);
C# :
class FindSum
{
int d(int n) { int s = 0, i = 1; for (; i <= n; ++i) s += n % i > 0 ? 0 : i; return s; }
static void Main(string[] args)
{
var f = new FindSum();
Console.WriteLine(string.Format("{0}, {1}, {2}, {3}, {4}", f.d(7), f.d(15), f.d(20), f.d(1), f.d(5)));
}
}
D :
writeln(d(7));
writeln(d(15));
writeln(d(20));
writeln(d(1));
writeln(d(5));
Java :
public class FindSum {
int d(int n){int s=0,i=1;for(;i<=n;++i)s+=n%i>0?0:i;return s;}
public static void main(String[] args) {
FindSum f = new FindSum();
System.out.println(String.format("%d, %d, %d, %d, %d", f.d(7), f.d(15), f.d(20), f.d(1), f.d(5)));
}
}
6@H.PWiz I think he means "the divisors of a number N" – benzene – 2017-09-08T00:18:47.383
I think you mean sum of divisors, aka, the sigma function?
– Stephen – 2017-09-08T00:18:48.257Sorry, i mean "The sum of the multiple of N". – Kevin Halley – 2017-09-08T00:19:35.420
@H.PWiz this is the sum of those, so I dunno – Stephen – 2017-09-08T00:21:11.987
@Stephen That seems like a trivial change to me – H.PWiz – 2017-09-08T00:21:53.623
Oh, and that question is restricted by time complexity, so maybe not. – H.PWiz – 2017-09-08T00:23:15.347
Closely related – Peter Taylor – 2017-09-08T06:25:45.853