| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | APLNARS | 250512T085243Z | RosLuP |
| 009 | APLDyalog Unicode | 250510T002703Z | Mat_rdv |
| 045 | Juby | 250506T191126Z | Jordan |
| 016 | Arturo | 221113T113426Z | chunes |
| 001 | Thunno 2 S | 230614T163431Z | The Thon |
| nan | 230214T104200Z | The Thon | |
| 062 | C | 170908T154516Z | HatsuPoi |
| 020 | K ngn/k | 221113T125633Z | oeuf |
| 003 | cQuents | 221113T034412Z | Stephen |
| nan | 220927T182512Z | bigyihsu | |
| 056 | Prolog SWI | 220926T211335Z | Aiden Ch |
| nan | Fig | 220926T180805Z | Seggan |
| 002 | MathGolf | 220926T092432Z | Kevin Cr |
| 027 | Ruby | 220926T035926Z | naffetS |
| 073 | tinylisp | 220224T214722Z | Giuseppe |
| 041 | Excel VBA | 170917T194815Z | Taylor R |
| 015 | Factor + math.primes.factors math.unicode | 220224T222242Z | chunes |
| 003 | Risky | 220129T063724Z | Dyad Han |
| 014 | K ngn/k | 220130T163344Z | coltim |
| 022 | Raku | 220105T024947Z | scpchick |
| 042 | Kotlin | 220129T111039Z | random p |
| 002 | Husk | 220105T013108Z | Natte |
| 001 | Vyxal s | 211208T004341Z | lyxal |
| 016 | TIBasic | 211208T003554Z | Youserna |
| 045 | Python 3 | 211207T183243Z | Larry Ba |
| 018 | x8616 machine code | 200915T173846Z | 640KB |
| 015 | GolfScript | 200915T163934Z | 2014MELO |
| 096 | Rockstar | 200914T182056Z | Shaggy |
| 042 | CasioBasic | 171011T080059Z | numberma |
| 026 | R | 170908T003930Z | Giuseppe |
| 043 | Shnap | 170908T011432Z | Socratic |
| 053 | Clojure | 170921T220313Z | NikoNyrh |
| 096 | BrainFlak | 170915T005534Z | MegaTom |
| 018 | Recursiva | 170908T061526Z | 0xffcour |
| 029 | Ruby | 170915T080705Z | G B |
| 009 | APL | 170910T214145Z | Adalynn |
| 004 | MY | 170910T213637Z | Adalynn |
| 042 | Axiom | 170909T045721Z | user5898 |
| 070 | Batch | 170908T234808Z | Neil |
| 014 | Mathematica | 170908T004032Z | ZaMoC |
| 044 | Javascript | 170908T005831Z | powelles |
| 006 | MATL | 170908T102533Z | Cinaski |
| 036 | PowerShell | 170908T134312Z | AdmBorkB |
| 030 | Haskell | 170908T134141Z | shooqie |
| nan | 170908T131543Z | auhmaan | |
| 002 | Jelly | 170908T003747Z | hyperneu |
| 016 | CJam | 170908T103932Z | Luis Men |
| 020 | Octave | 170908T103054Z | Luis Men |
| 002 | Neim | 170908T101750Z | Okx |
| 002 | Gaia | 170908T092841Z | Mr. Xcod |
| 005 | Husk | 170908T074322Z | Mr. Xcod |
| 051 | Java OpenJDK 8 | 170908T075545Z | Nevay |
| 002 | Ohm v2 | 170908T073736Z | Mr. Xcod |
| 017 | QBIC | 170908T072209Z | steenber |
| 006 | Pyth | 170908T071354Z | Mr. Xcod |
| 009 | Add++ | 170908T063733Z | caird co |
| 002 | Brachylog | 170908T063114Z | Fatalize |
| 023 | J | 170908T061016Z | Jonah |
| 073 | VBA Excel | 170908T032235Z | remoel |
| 045 | C gcc | 170908T015544Z | tsh |
| 041 | Python 2 | 170908T054352Z | Chas Bro |
| 036 | Bash + GNU utilities | 170908T052119Z | Digital |
| 005 | Pari/GP | 170908T053746Z | alephalp |
| 023 | x8664 Machine Code | 170908T050350Z | Cody Gra |
| 044 | Python | 170908T014042Z | tsh |
| nan | Perl 5 | 170908T040519Z | Xcali |
| 002 | RProgN 2 | 170908T021119Z | ATaco |
| 031 | JavaScript | 170908T014257Z | tsh |
| 003 | Japt | 170908T005458Z | powelles |
| 002 | 05AB1E | 170908T002959Z | H.PWiz |
APL(NARS), 3 chars
11π
This above is builtin, without bultin would be something as:
{+/k/⍨∼×⍵∣⍨k←⍳⍵}
16 chars for one algo not scale well with increase of the number of input.
test:
{+/k/⍨∼×⍵∣⍨k←⍳⍵}20
42
11π20
42
11π1
1
APL(Dyalog Unicode), 9 bytes SBCS
+/∘⍸0=⍳|⊢
Explanation
⊢ right argument (let it be N)
| modulo
⍳ numbers from 1 to N
=
0
0=⍳|⊢ boolean mask for divisors of N
⍸ vector of indices (from 1 to N) of 1s in the mask
∘ function composition
/ reduction
+
+/ sum
+/∘⍸0=⍳|⊢ solution
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;}
C++ optimization by c and c-- : 53 52 bytes
int f(int n,int i=0){return++i<n?f(n,i)+i*!(n%i):n;}
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)));
}
}
K (ngn/k), 20 bytes
{+/t(*=(t:1+!x)!'x)}
I honestly didn't know how I manage to make a for-each that find the divisors, lol.
Explanations:
{+/t(*=(t:1+!x)!'x)} Main function. x is input
( 'x) For x amount of times
! Modulo it with
( !x) Range, create a list of [0..x-1]
1+ + 1 to each number in list to turn it into [1..x]
t: Assign it to variable t
Now, we got the modulo results of each number from [1..x],
we now need to
= Group them into a dictionary
* And get the value of the first key
i.e. the occurence positions of the 0s
(Implicit at)
t The variable t
+/ Sum
cQuents, 3 bytes
Uz$
Explanation
implicit mode sequence - print nth term in sequence given input n
U sum ( )
z divisors ( )
$ current term
Go, 62 bytes
func p(n int)(p int){i:=1
for;i<=n;i++{if n%i<1{p+=i}}
return}
-2 bytes compared to my answer to Product of Divisors via use of Go's 0-initialization for variables.
Prolog (SWI), 60 56 bytes
-4 bytes thanks to @Steffan
N+X:-bagof(M,(between(1,N,M),N mod M<1),L),sumlist(L,X).
MathGolf, 2 bytes
─Σ
One of many 2-bytes answers in a code-golf language.
Explanation:
─ # Get a list of divisors of the (implicit) input-integer
Σ # Sum them together
# (after which the entire stack is output implicitly as result)
tinylisp, 74 73 bytes
(load library
(d D(q((K N)(i K(a(i(mod N K)0 K)(D(s K 1)N))0
(q((N)(D N N
-1 byte thanks to DLosc.
And without library just for fun:
tinylisp, 97 90 bytes
(d D(q((N K)(i(l N K)N(D(s N K)K
(d F(q((K N)(i K(a(i(D N K)0 K)(F(s K 1)N))0
(q((N)(F N N
-7 bytes thanks to DLosc.
Excel VBA, 41 Bytes
Anonymous VBE immediate window function that takes input from [A1] and outputs to the VBE immediate window
For i=1To[A1]:s=s-i*([A1]mod i=0):Next:?s
Excel, 41 Bytes
Worksheet formula that takes input from [A1] and outputs to the caller
Requires MS Excel Version 16.0 or later for access to Let(...) function
=LET(a,SEQUENCE(A1),SUM((MOD(A1,a)=0)*a))
K (ngn/k), 14 bytes
{+/&~1!x%!1+x}
!1+xgenerate sequence from1..xx%divide the input by each term in this range&~1!identify values in the range that evenly divide the input+/take (and implicitly return) their sum
Raku, 25 23 22 bytes
thanks ovs for 2 byte improvement
also JoKing for 1 byte improvement
{sum grep $_%%*,1..$_}
declare anonymous block ($_ implicitly declared)
filter (grep) numbers from 1 to $_ inclusive using the whatever variable (*) that are divisible by $_
get the sum of that list
TI-Basic, 16 bytes
sum(seq(Inot(fPart(Ans/I)),I,1,Ans
Takes input in Ans. Output is stored in Ans and displayed.
x86-16 machine code, 18 bytes
00000000: 89c1 9988 0e09 0150 d40a 7502 02d4 58e2 .......P..u...X.
00000010: f2c3 ..
Listing:
89 C1 MOV CX, AX ; input number as loop counter
99 CWD ; zero DX as running sum
DIVLOOP:
88 0E 0109 MOV BYTE PTR[AAM1+1], CL ; move loop counter to divisor
50 PUSH AX ; save input number
AAM1:
D4 0A AAM ; ZF if (AL % CL == 0), AH = quotient
75 02 JNZ END_LOOP ; if not ZF, continue loop
02 D4 ADD DL, AH ; otherwise add to running sum
END_LOOP:
58 POP AX ; restore input number
E2 F2 LOOP DIVLOOP ; loop until CL == 0
C3 RET ; return to caller
Callable function, input N in AX, result in DX.
Inspired by @CodyGray's excellent answer, and the comment "It sure seems like there should be a way to make this shorter" I just had to try!
As Cody mentioned, Using DIV/IDIV is inconvenient because it clobbers two registers including the dividend. Another is that the ZF flag is set when the quotient is 0, however in this case we're only interested in when the remainder is 0. Enter AAM, a trusty and underrated byte-sized division/modulo instruction that can be golfy. Even though it does clobber the dividend just like DIV, it will set ZF when the remainder is 0 which is what we want here. It's downside is that the divisor is encoded in the instruction opcode, which can be modified at runtime at a cost of 4 bytes (in real mode at least).
GolfScript, 15 bytes
~.,{.3$\%!*+}*+
~ # Parses the input to integer
., # Makes an array with numbers from 0 to (N-1)
{.3$\%!*+} # This block goes to the top of the stack without being executed
* # Folds
+ # Adds the input and the result
What the block does:
. # Duplicates the number of the array
3$ # Makes a copy of N
\%! # Tests if the number of the array is a divisor of N
* # Multiplies, resulting in the divisor if it was divisible or 0 if it wasn't
+ # Adds to the acumulator
Rockstar, 96 bytes
listen to N
X's0
T's0
while N-X
let X be+1
let D be N/X
turn up D
let T be+D is N/X and X
say T
Try it here (Code will need to be pasted in)
Casio-Basic, 42 bytes
sum(seq(piecewise(n/x=int(n/x),x,0),x,1,n
The piecewise acts as a shorter If statement. seq loops values of x from 1 to n, and the piecewise returns x if the number is a factor, otherwise it returns 0. sum adds the whole list together to output the total.
41 bytes for the function, +1 to add n in the parameters box.
R, 31 26 bytes
function(N)(x=1:N)%*%!N%%x
Returns a 1x1 matrix.
Computes !N%%x maps elements d of 1:N by: d->(1 if d divides N, 0 otherwise)
Then x%*%x!N%%x is the matrix product of 1:N which results in the sum of x where !N%%x is 1. Neat! Technically a port of Luis Mendo's Octave answer but I only saw that after I thought of this.
R+ numbers, 14 bytes
numbers::Sigma
Shnap, 44 43 bytes
-1 bye thanks to Mr. Xcoder (lol I was outgolfed in my own language)
$n return:{s=0for d:range(n+1)if n%d<1s+=d}
This is a function ($ starts a function in Shnap).
Explanation:
$ n //Start function with parameter n
return: { //Technically, we are returning a scope-block, which evaluates to the last statement run
s = 0 //Our result
for d : range(n+1) //For each value in the iterator range(n+1)
if n % d < 1 // If n is divisible by d
s += d // Add d to the sum
// Since (s += d) returns (s + d), and a scope-block returns the last run statement, this will be the last statement and equal to our result
}
Noncompeting, 19 bytes
After many language updates, this can now be reduced to a measly 19 bytes:
$n=>sum(factors(n))
Clojure, 53 bytes
#(apply +(for[i(range 1(inc %)):when(=(mod % i)0)]i))
Brain-Flak, 96 bytes
((({})<>){<(([()]{})){<>(({})(<()>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})){((<{}{}>))}}{}>{}})
Explanation:
Now outdated by improvements.
The heart of the algorithm is this:
({}(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})) turns |N, M...| into |N mod M, M...|
{((<{}{}>))} if the top of stack is not zero, replace it and the second with zero
That is a modification on mod that will give us M if it is a factor of N and 0 otherwise. Full code is below.
((({})<>) place input, N on both stacks
{ Loop to find factors
<
(([()]{})) Decrement and Duplicate; get next factor to check
{ if not zero
(<>({})<>) Copy N from other stack
({}(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})){((<{}{}>))} Code explained above
}
{} drop the zero
>
{} add the factor
}) push the sum
Recursiva, 20 18 bytes
smBa++'%'Va'a:0!a'
Explanation:
smBa++'%'Va'a:0!a' - Input to a; 20
s - sum up; 42
m - map with; [1, 0, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15]
Ba - Range; [1,2..20]
++'%'Va'a:0!a' - function, evaluates to 0 if param cannot divide a i.e. 20
APL, 9 bytes
+/⍳×0=⍳|⊢
How? (input n)
⍳|⊢,[n mod 1, ..., n mod n]0=,[0 == n mod 1, ... 0 == n mod n]⍳×,[1*(0 == n mod 1), ..., n*(0 == n mod n)]+/,sum([1*(0 == n mod 1), ..., n*(0 == n mod n)])
MY, 4 bytes
ωḊΣ↵
How?
It's really simple: ω is the argument, Ḋ is divisors, Σ is sum, ↵ is output. I thought I already answered this challenge, for some reason.
Axiom, 42 bytes
f(x:PI):PI==(x=1=>1;reduce(+,divisors(x)))
results
(19) -> [[i,f(i)] for i in [7,15,20,42,1,5] ]
(19) [[7,8],[15,24],[20,42],[42,96],[1,1],[5,6]]
Type: List List PositiveInteger
Batch, 70 bytes
@set/ai=%2+1,s=%3+i*!(%1%%i)
@if not %i%==%1 %0 %1 %i% %s%
@echo %s%
Alternative solution, also 70 bytes:
@set s=0
@for /l %%i in (1,1,%1)do @set/as+=%%i*!(%1%%%%i)
@echo %s%
Mathematica, 14 bytes
Tr@Divisors@#&
or an answer by @Loki
Mathematica, 17 bytes
DivisorSum[#,#&]&
Javascript, 54 44 bytes
n=>[...Array(x=n)].reduce(y=>y+!(n%x)*x--,0)
Saved 10 bytes thanks to Shaggy
Try it online!
const f = n=>[...Array(x=n)].reduce(y=>y+!(n%x)*x--,0)
console.log(f(7))
console.log(f(15))
console.log(f(20))
console.log(f(1))
console.log(f(5))
MATL, 6 bytes
t:\~fs
-4 bytes thanks to @LuisMendo
10 bytes
My previous solution using a loop
:"G@\~@*vs
3 bytes
Using built-in
Z\s
PowerShell, 36 bytes
param($a)1..$a|%{$j+=$_*!($a%$_)};$j
Explanation
param($a)1..$a|%{$j+=$_*!($a%$_)};$j
param($a) # Takes input $a
1..$a|%{ }; # For-loop from 1 up to $a
$a%$_ # Modulo, if this is zero we've hit a divisor
!( ) # Take the Boolean-not of that. If a divisor, it's 1
$_* # Multiply the current number by that Boolean
# Only if it's a divisor will this be non-zero
$j+= # Add it into our accumulator
$j # Output our accumulator
C#, 56 bytes
Data
- Input
Int32iA number - Output
Int32The sum of the divisors of the number
Golfed
i=>{int s=0,d=1;for(;d<=i;d++)s+=i%d<1?i/d:0;return s;};
Ungolfed
i => {
int
s = 0,
d = 1;
for( ; d <= i; d++ )
s += i % d < 1 ? i / d : 0;
return s;
};
Ungolfed readable
// Takes an int
i => {
// Initializes the sum 's' and divider 'd' vars
int
s = 0,
d = 1;
// Cycles each number lower than the the number 'i'
for( ; d <= i; d++ )
// Sums the result of the division between 'i' and 'd' if the modulus is 0
s += i % d < 1 ? i / d : 0;
// Returns the sum
return s;
};
Full code
using System;
using System.Collections.Generic;
namespace TestBench {
public class Program {
// Methods
static void Main( string[] args ) {
Func<Int32, Int32> f = i => {
int s = 0, d = 1;
for( ; d <= i; d++ )
s += i % d < 1 ? i / d : 0;
return s;
};
List<Int32>
testCases = new List<Int32>() {
7,
15,
20,
42,
1,
5,
};
foreach( Int32 testCase in testCases ) {
Console.WriteLine( $" INPUT: {testCase}\nOUTPUT: {f( testCase )}" );
}
Console.ReadLine();
}
}
}
Releases
- v1.0 -
56 bytes- Initial solution.
Notes
- None
CJam, 16 bytes
ri:X{)_X\%!*}%:+
Explanation
ri e# Read integer, n
:X e# Write to variable X
{ }% e# Map this block over the array [0 1 ... n-1]
) e# Increment current value. Will give 1, 2, ..., n
_ e# Duplicate
X e# Push input
\ e# Swap
% e# Modulus
! e# Negate
* e# Multiply
:+ e# Sum of array. Implicitly display
Husk, 5 bytes
ṁΠuṖp
How?
ṁΠuṖp - Full program, implicit input.
p - Prime factors.
Ṗ - Powerset.
u - Remove duplicates.
ṁΠ - Get the product of each list, sum and implicitly output.
Thanks to Zgarb for the suggestions in chat!
QBIC, 17 bytes
[:|~b%a|\p=p+a}?p
Explanation
[:| FOR a = 1; a <= b (read from cmd line); a++
~b%a| IF b modulo a has a remainder THEN - empty block -
\p=p+a ELSE add divisor 'a' to running total 'p'
} END IF, NEXT
?p PRINT p
Pyth, 6 bytes
s*M{yP
Pyth doesn't have a built-in for divisors, so I think this is reasonable.
Explanation
s*M{yP - Full program with implicit input.
P - The prime factors of the input.
y - The powerset of its prime factors.
{ - Deduplicate.
*M - Map with multiplication.
s - Sum.
- Implicitly display the result.
Given 20, for instance, this is what our program does after each instruction:
P:[2, 2, 5].y:[[], [2], [2], [5], [2, 2], [2, 5], [2, 5], [2, 2, 5]].{:[[], [2], [5], [2, 2], [2, 5], [2, 2, 5]].*M:[1, 2, 5, 4, 10, 20].s:42.
Add++, 9 bytes
D,f,@,dFs
I clearly got here too late. This defines a function that gets the factors, then sums them.
J, 23 bytes
[:+/](([:=&0]|[)#])1+i.
For J fans, there is a clever 13 byte solution: >:@#.~/.~&.q: but since it wasn't my invention I'm not posting it as my official answer.
My own solution simply filters 1..n, finding divisors, then sums them. The crux of it is the dyadic fork
](([:=&0]|[)#])
Note that in this context ] is 1..n, and [ is n itself. Hence ]|[ are the remainders when dividing each element of 1..n into n, and =&0 tells you if they're equal to 0.
VBA (Excel), 73 bytes
a=Cells(1,1)
x=1
While x<=a
If a Mod x = 0 Then b=b+x
x=x+1
Wend
MsgBox b
Bash + GNU utilities, 36
bc<<<`seq -f"n=%g;a+=n*!$1%%n;" $1`a
Pure Bash, 41
for((;++i<=$1;a+=$1%i?0:i))
{
:
}
echo $a
I first tried a fancy bash expansion answer, but it ended up being longer than the simple loop above:
echo $[$(eval echo +\\\(n={1..$1},$1%n?0:n\\\))]
x86-64 Machine Code, 23 bytes
89 F9 89 FE EB 0D 89 F8 99 F7 F1 85 D2 99 0F 44 D1 01 D6 E2 F1 96 C3
The above bytes of code define a function that accepts a single integer, N, and returns the sum of its multiples as a result.
The single parameter is passed in the EDI register, consistent with the System V AMD64 ABI (as used on *nix-style systems). The result is returned in the EAX register, as with all x86 calling conventions.
The algorithm is a very straightforward one, similar to many of the other submissions in other languages. We loop N times, each time computing the modulo and adding that to our running total.
Ungolfed assembly mnemonics:
; unsigned SumOfMultiples(unsigned N /* (EDI) */)
mov ecx, edi ; make copy of input N, to be used as our loop counter
mov esi, edi ; make copy of input N, to be used as our accumulator
jmp CheckEnd ; jump directly to 'CheckEnd'
AddModulo:
mov eax, edi ; make copy of input N, to be used as input to DIV instruction
cdq ; short way of setting EDX to 0, based on EAX
div ecx ; divide EDX:EAX by ECX, placing remainder in EDX
test edx, edx ; test remainder, and set ZF if it is zero
cdq ; again, set EDX to 0, without clobbering flags
cmovz edx, ecx ; set EDX to ECX only if remainder was zero (EDX = ZF ? 0 : ECX)
add esi, edx ; add EDX to accumulator
CheckEnd:
loop AddModulo ; decrement loop counter (ECX), and keep looping if it != 0
xchg eax, esi ; move result from accumulator (ESI) into EAX
ret ; return, with result in EAX
It sure seems like there should be a way to make this shorter, but I can't see it. Computing modulo on x86 takes quite a bit of code, since you do it using the DIV (or IDIV) instruction, and both of those use fixed input registers (EDX and EAX), the values of which get clobbered (because they receive the results, the remainder and quotient, respectively).
The only real tricks here are pretty standard golfing ones:
- I've structured the code in a somewhat unusual way so that I can use the CISC-style
LOOPinstruction, which is basically just a combination ofDEC+JNZwith theECXregister as the implicit operand. - I'm using
XCHGat the end instead ofMOVbecause the former has a special 1-byte encoding whenEAXis one of the operands. - I use
CDQto zero outEDXin preparation for the division, even though for unsigned division you would ordinarily just zero it using aXOR. However,XORis always 2 bytes, whileCDQis only 1 byte. I useCDQagain a second time inside of the loop to zeroEDX, before theCMOVZinstruction. This works because I can be guaranteed that the quotient of the division (inEAX) is always unsigned, so a sign-extension intoEDXwill setEDXequal to 0.
Python, 44 bytes
lambda k:sum(i*(k%i<1)for i in range(1,1+k))
- Thanks to Stephen, save 1 byte by removing whitespace.
- Thanks to Jonathan Frech, save another 1 byte by changing if to multiply.
JavaScript, 31 bytes
f=(n,i=n)=>i&&!(n%i)*i+f(n,i-1)