[Question]
  [
Write a program that displays on the screen the sum of the divisors of a number (1 ≤ N ≤ 100) entered by the user in the range of 1 to N.
This is [OEIS A000203](https://oeis.org/A000203).
---
Examples:
**Input**: 7
```
7 / 1 = 7
7 / 7 = 1
7 + 1 = 8
```
**Output:** 8
---
**Input:** 15
```
15 / 1 = 15
15 / 3 = 5
15 / 5 = 3
15 / 15 = 1
15 + 5 + 3 + 1 = 24
```
**Output:** 24
---
**Input:** 20
```
20 / 1 = 20
20 / 2 = 10
20 / 4 = 5
20 / 5 = 4
20 / 10 = 2
20 / 20 = 1
20 + 10 + 5 + 4 + 2 + 1 = 42
```
**Output:** 42
---
**Input:** 1
```
1 / 1 = 1
```
**Output:** 1
---
**Input:** 5
```
5 / 1 = 5
5 / 5 = 1
5 + 1 = 6
```
**Output:** 6
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 2 bytes
```
ÑO
```
[Try it online!](https://tio.run/##MzBNTDJM/f//8ET///@NDAA "05AB1E – Try It Online")
### How?
```
Ñ    Divisors
 O   Sum
```
[Answer]
# 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
```
**[Try it online!](https://tio.run/##bY5NasMwEIXX1ikGl4Jd6kZS/YvbLBrHl4i6MLLjCBK5WDIISq9eV8UKycKLYeY9vpk3POo5n2c@SKWBn5oRTJ7uhrY7fMI7@MzkBTN14fqemf0HM7hyOmem@O@ZLWK9hJmKLh6umYljq62PbVWp3aULV9h59@qXSOlGCw6TVKKXXQvBk02vjQyDqxXaL9xH5fwgJD9PbQdvSrdieDltERJSw6URMgjRN/K@RquPgQ8Z09GW6ceWSf8ZlqsBZGFYepsN5DeSJGskSRxJ4xtK8RpKsUNjepdPVvOJQ8kduZoP1/wUecgbOz2NEnCJfuZffjw3vZqjSxr/AQ)**
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 `LOOP` instruction, which is basically just a combination of `DEC`+`JNZ` with the `ECX` register as the implicit operand.
* I'm using `XCHG` at the end instead of `MOV` because the former has a special 1-byte encoding when `EAX` is one of the operands.
* I use `CDQ` to zero out `EDX` in preparation for the division, even though for unsigned division you would ordinarily just zero it using a `XOR`. However, `XOR` is always 2 bytes, while `CDQ` is only 1 byte. I use `CDQ` again a second time inside of the loop to zero `EDX`, before the `CMOVZ` instruction. This works because I can be guaranteed that the quotient of the division (in `EAX`) is always unsigned, so a sign-extension into `EDX` will set `EDX` equal to 0.
[Answer]
# C (gcc), 45 bytes
```
i,s;f(n){for(s=i=n;--i;)s+=n%i?0:i;return s;}
```
[Try it online!](https://tio.run/##Xc/RCoIwGIbh4@0qhiFs5HITzWhpN@JJTFc/1IxtHonXbh0YiYcfPPDyaX7Xep4h8cpQy0bTO@orqKziHBTz@8rGcBVnUK4Lg7PEq2negdXPoe3IxYcW@sOjxhhsIK8bWMrwiNHbfbehESmbwOsmxG1jo4QYSkrGFEpTcvojWWyQLBaU5X@ViY3KxKLybBWU26BclFyhbZD8gkeMMFqeCoWn@QM "C (gcc) – Try It Online")
[Answer]
## 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)));
    }
}
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 3 bytes
```
â)x
```
[Try it online!](https://ethproductions.github.io/japt/?v=1.4.5&code=4il4&input=MjA=)
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 2 bytes
```
f+
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/P037/38jg/9RAA "Brachylog – Try It Online")
### Explanation
```
f       Factors
 +      Sum
```
[Answer]
# Mathematica, 14 bytes
```
Tr@Divisors@#&   
```
or an answer by @Loki
# Mathematica, 17 bytes
```
DivisorSum[#,#&]&
```
[Answer]
# [Shnap](https://github.com/ShnapLang/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).
[Try it online!](https://tio.run/##K87ISyz4n2b7XyVPoSi1pLQoz6q62NYgLb9IIcWqKDEvPVUjT9tQMzNNIU81xcawWNs2pfY/SDbaXMfQVMfIQMdQxzRWoaAoM68kJ08jTSOzRFPzPwA)
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))
```
[Try it online!](https://tio.run/##K87ISyz4n2b7XyXP1q64NFcjLTG5JL@oWCNPU/N/Wn6RQrS5jqGpjpGBjqGOaaxCQVFmXklOnkaaRmYJUAEA)
[Answer]
# [Risky](https://github.com/Radvylf/risky), 3 bytes
```
+/?+??
```
[Try it online!](https://radvylf.github.io/risky?p=WyIrLz8rPz8iLCJbN10iLDBd)
[Answer]
# 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.
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 5 bytes
```
sigma
```
[Try it online!](https://tio.run/##K0gsytRNL/ifpmCr8L84Mz038X9afpFGHpBrqKNgaGCgo1BQlJlXAhRRUtC1AxJpGnmampr/AQ "Pari/GP – Try It Online")
[Answer]
# J, 23 bytes
```
[:+/](([:=&0]|[)#])1+i.
```
[Try it online!](https://tio.run/##y/r/P81WTyHaSls/VkMj2spWzSC2JlpTOVbTUDtTjys1OSNfIU3B0BTGMjKAi/3/DwA "J – Try It Online")
For J fans, there is a [clever 13 byte solution](http://code.jsoftware.com/wiki/Essays/Divisors#Sum_of_Divisors): `>:@#.~/.~&.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.
[Answer]
# [Pyth](https://pyth.readthedocs.io), 6 bytes
```
s*M{yP
```
**[Try it here!](https://pyth.herokuapp.com/?code=s%2aM%7ByP&input=20&debug=0)**
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`.
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), ~~53~~ 51 bytes
```
n->{int s=0,i=0;for(;i++<n;)s+=n%i<1?i:0;return s;}
```
[Try it online!](https://tio.run/##lY5BS8NAEIXv@RVzERJil1QQwW0Ujx6sh@JJPIxpUibdzi67s4VQ8tvjVoJn847D9@Z9PZ5xZV3L/f44ufhtqIHGYAjwhsRwySAlCEq6d8RooE8NFYWM6iI3QpbVK8sHox/eXetRrIct1BOvni7EAqGubqmudGd9rqksN6yLUNZ8Q5v1Mz1W2rcSPUPQ43Qd09nv5uwyT58t7eGUjPKdeOLD5xegP4RiFrxmNwRpT8pGUS4hYjjfKnTODC8hCeYPRaH/Da/vl9B31aLfS@A/jzEbpx8 "Java (OpenJDK 8) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 2 bytes
```
Æs
```
[Try it online!](https://tio.run/##y0rNyan8//9wW/H////NAQ "Jelly – Try It Online")
Built-in that does exactly as wanted.
[Answer]
# [Haskell](https://www.haskell.org/), 30 bytes
```
f n=sum[i|i<-[1..n],n`mod`i<1]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hz7a4NDc6sybTRjfaUE8vL1YnLyE3PyUh08Yw9n9uYmaegq1CQWlJcEmRT56CikJxRn45kMpNLFDQiMlT0LVT0AAL5WkqaGsrKIEElEAsiKgG0HxNTU2FaHMdBUNTHQUjAyCto2Aa@x8A "Haskell – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 6 bytes
```
t:\~fs
```
[Try it online!](https://tio.run/##y00syfn/v8Qqpi6t@P9/IwMA "MATL – Try It Online")
-4 bytes thanks to @LuisMendo
# 10 bytes
My previous solution using a loop
```
:"G@\~@*vs
```
[Try it online!](https://tio.run/##y00syfn/30rJ3SGmzkGrrPj/fyMDAA "MATL – Try It Online")
# 3 bytes
Using built-in
```
Z\s
```
[Try it online!](https://tio.run/##y00syfn/Pyqm@P9/I4P/AA "MATL – Try It Online")
[Answer]
# Javascript, ~~54~~ 44 bytes
```
n=>[...Array(x=n)].reduce(y=>y+!(n%x)*x--,0)
```
Saved 10 bytes thanks to [Shaggy](https://codegolf.stackexchange.com/users/58974/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))
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 96 bytes
```
((({})<>){<(([()]{})){<>(({})(<()>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})){((<{}{}>))}}{}>{}})
```
[Try it online!](https://tio.run/##LYsxCoBADAS/sylsrCxCPnJYnIUgioXtkrfHPRFCyM5Otqcf97Rf/awCwDQPowMNtirpjg/DYWFq@UWj9nBEkqmR15gifw2nqF5GGVKsal5e "Brain-Flak – Try It Online")
## 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
```
[Answer]
# [R](https://www.r-project.org/), ~~31~~ 26 bytes
```
function(N)(x=1:N)%*%!N%%x
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP0/DT1OjwtbQyk9TVUtV0U9VteJ/moahgeZ/AA "R – Try It Online")
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](https://codegolf.stackexchange.com/a/142151/67312) but I only saw that after I thought of this.
# [R](https://www.r-project.org/)+ numbers, 14 bytes
```
numbers::Sigma
```
[Try it online!](https://tio.run/##K/r/P680Nym1qNjKKjgzPTfx/38A "R – Try It Online")
[Answer]
# TI-Basic, 16 bytes
```
sum(seq(Inot(fPart(Ans/I)),I,1,Ans
```
Takes input in `Ans`. Output is stored in `Ans` and displayed.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `s`, 1 byte
```
K
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJzIiwiIiwiSyIsIiIsIjciXQ==)
`K∑` flagless. `K` just gets the divisors of a number.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 2 bytes
```
ΣḊ
```
[Try it online!](https://tio.run/##yygtzv7//9zihzu6/v//b2QAAA "Husk – Try It Online")
[Answer]
# [Raku](https://www.raku.org/), ~~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
```
[Try it online!](https://tio.run/##NY3NCoJAGEX331NcRMMJHRpRC8SeoqUggUMM@YejRojPPn0u2p5zOXfUU5u77otTY9bYLh1Kt4VKSr8W8jXpMfTrIDgLyWp3i9V4aDsXRK3ptZXvVXbPMYzvqEzfRKgOjI0ATobMGA3LLFDiMPIzTI0tWBsL08v/aQQeRfBmbmPj0u7RLtwVN1IZkpSSC9KEFBRlyH8 "Raku – Try It Online")
[Answer]
# 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))
```
[Answer]
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/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
```
[Try it online!](https://tio.run/##FcohEoAgEAXQ7il@/NsgeAMaMxROgEPZGRQFC6dHDK@9V69RtN9zstSUUfRoqY2NGY4P6RGECs9E5VkzAryYhY4dHlaCiPl7/PvKbpUwGWH3@QE "tinylisp – Try It Online")
-1 byte thanks to DLosc.
And without `library` just for fun:
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/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
```
[Try it online!](https://tio.run/##Hcw9DoUwDAPgnVN4dDY6cIOKJVIWTlDEUqnv8btw@pJ2s@IvfvL/Lfk@auWGyJM0qDCzoAVj5N2TDg7mBhTmAMrkLPZybD@zS0UQExkbXvqa351YZdnThpLXK12vDPylAwsYnh1hEqkf "tinylisp – Try It Online")
-7 bytes thanks to DLosc.
[Answer]
# JavaScript, 31 bytes
```
f=(n,i=n)=>i&&!(n%i)*i+f(n,i-1)
```
[Answer]
# Bash + GNU utilities, 36
```
bc<<<`seq -f"n=%g;a+=n*!$1%%n;" $1`a
```
[Try it online](https://tio.run/##S0oszvj/PynZxsYmoTi1UEE3TSnPVjXdOlHbNk9LUcVQVTXPWklBxTAh8f///4amAA).
---
# Pure Bash, 41
```
for((;++i<=$1;a+=$1%i?0:i))
{
:
}
echo $a
```
[Try it online](https://tio.run/##S0oszvj/Py2/SEPDWls708ZWxdA6URtIqmbaG1hlampyVXNZcdVypSZn5CuoJP7//9/QFAA).
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\\\))]
```
[Answer]
# [Python 2](https://docs.python.org/2/), 41 bytes
```
f=lambda n,i=1:i<=n and(n%i<1)*i+f(n,i+1)
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIU8n09bQKtPGNk8hMS9FI08108ZQUytTO00DKKNtqPk/JTVNwU0jT9OKi7OgKDOvBKgDKKfJxeWmYa4JJAxNQaSRAYgEM4F6AA "Python 2 – Try It Online")
[Answer]
# 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
```
]