28
\$\begingroup\$

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.


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

Nissa
3,6641 gold badge19 silver badges45 bronze badges
asked Sep 8, 2017 at 0:14
\$\endgroup\$
7
  • 6
    \$\begingroup\$ @H.PWiz I think he means "the divisors of a number N" \$\endgroup\$ Commented Sep 8, 2017 at 0:18
  • \$\begingroup\$ I think you mean sum of divisors, aka, the sigma function? \$\endgroup\$ Commented Sep 8, 2017 at 0:18
  • \$\begingroup\$ Sorry, i mean "The sum of the multiple of N". \$\endgroup\$ Commented Sep 8, 2017 at 0:19
  • \$\begingroup\$ @H.PWiz this is the sum of those, so I dunno \$\endgroup\$ Commented Sep 8, 2017 at 0:21
  • \$\begingroup\$ @Stephen That seems like a trivial change to me \$\endgroup\$ Commented Sep 8, 2017 at 0:21

70 Answers 70

1
2 3
22
\$\begingroup\$

05AB1E, 2 bytes

ÑO

Try it online!

How?

Ñ Divisors
 O Sum
answered Sep 8, 2017 at 0:29
\$\endgroup\$
2
  • \$\begingroup\$ Polyglot with 2sable \$\endgroup\$ Commented Sep 8, 2017 at 9:31
  • 16
    \$\begingroup\$ ÑO - Rejecting the challenge and winning at the same time. That's pretty badass. \$\endgroup\$ Commented Sep 8, 2017 at 20:26
7
\$\begingroup\$

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!

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.
answered Sep 8, 2017 at 5:03
\$\endgroup\$
6
\$\begingroup\$

C (gcc), 45 bytes

i,s;f(n){for(s=i=n;--i;)s+=n%i?0:i;return s;}

Try it online!

Cody Gray
3,49717 silver badges28 bronze badges
answered Sep 8, 2017 at 1:55
\$\endgroup\$
2
  • \$\begingroup\$ If you're OK with undefined behaviour, you can save a few bytes by replacing that return s; with s=s;. It comes out to 40 bytes. \$\endgroup\$ Commented May 11 at 15:41
  • \$\begingroup\$ Turns out you can shave 1 more byte by making it recursive. 39 bytes \$\endgroup\$ Commented May 11 at 16:13
4
\$\begingroup\$

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)));
 }
}
answered Sep 8, 2017 at 15:45
\$\endgroup\$
2
  • \$\begingroup\$ A few things: First, I don't think you need parentheses around the n%i/n%i!=0 in any of the languages. Second, your first solution should be able to have n%i>0 instead of n%i!=0. Third, D's solution can be T d(T)(T n){T s,i=1;for(;i<=n;++i)s+=n%i?0:i;return s;} by abusing the template system and default values. \$\endgroup\$ Commented Sep 9, 2017 at 13:51
  • \$\begingroup\$ 52 bytes, C++ \$\endgroup\$ Commented Nov 24, 2022 at 23:09
3
\$\begingroup\$

Japt, 3 bytes

â)x

Try it online!

answered Sep 8, 2017 at 0:54
\$\endgroup\$
3
  • \$\begingroup\$ Alternative: â x \$\endgroup\$ Commented Sep 8, 2017 at 22:11
  • \$\begingroup\$ @Mr.Xcoder: not really an alternative; it's doing the exact same thing - only difference is the choice of parenthesising. \$\endgroup\$ Commented Oct 10, 2017 at 16:57
  • \$\begingroup\$ Or with the flag -x, it could be one byte \$\endgroup\$ Commented Apr 20, 2019 at 19:30
3
\$\begingroup\$

Brachylog, 2 bytes

f+

Try it online!

Explanation

f Factors
 + Sum
answered Sep 8, 2017 at 6:31
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 14 bytes

Tr@Divisors@#& 

or an answer by @Loki

Mathematica, 17 bytes

DivisorSum[#,#&]&
answered Sep 8, 2017 at 0:40
\$\endgroup\$
7
  • \$\begingroup\$ @Jennymathy Very nice, thanks! An equivalent and funny way to write it is also: DivisorSum[#, # &] & \$\endgroup\$ Commented Sep 8, 2017 at 18:15
  • \$\begingroup\$ @Jennymathy Hmm, this is even better: Total@Divisors@ is only 15 characters long! And it works: eg Total@Divisors@15 gives 24 as expected. Mathematica FTW :) \$\endgroup\$ Commented Sep 8, 2017 at 19:27
  • 2
    \$\begingroup\$ @Loki and Tr@Divisors@#& even better ;-) \$\endgroup\$ Commented Sep 8, 2017 at 19:29
  • 1
    \$\begingroup\$ @Loki the program must be a function f= that takes an input f[x] that's why I present it in this way.Welcome to PPCG \$\endgroup\$ Commented Sep 8, 2017 at 19:36
  • 4
    \$\begingroup\$ You can use Tr@*Divisors to shave off a byte. \$\endgroup\$ Commented Sep 9, 2017 at 1:52
3
\$\begingroup\$

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!

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!

answered Sep 8, 2017 at 1:14
\$\endgroup\$
2
  • 1
    \$\begingroup\$ ==0 is <1 (43 bytes) \$\endgroup\$ Commented Sep 8, 2017 at 9:22
  • \$\begingroup\$ @Mr. Xcoder thanks... I was outgolfed... In my own language... Which isn't even esoteric xD \$\endgroup\$ Commented Sep 8, 2017 at 10:51
3
\$\begingroup\$

Risky, 3 bytes

+/?+??

Try it online!

rydwolf
19.3k2 gold badges90 silver badges180 bronze badges
answered Jan 29, 2022 at 6:37
\$\endgroup\$
2
\$\begingroup\$

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.
answered Sep 8, 2017 at 1:40
\$\endgroup\$
4
  • \$\begingroup\$ You could also write it as lambda k:sum(-~i*(k%-~i<1)for i in range(k)) \$\endgroup\$ Commented Apr 16, 2022 at 11:04
  • \$\begingroup\$ 41 bytes. \$\endgroup\$ Commented Sep 26, 2022 at 14:05
  • \$\begingroup\$ @97.100.97.109 Maybe that is different enough to post as another answer. \$\endgroup\$ Commented Sep 27, 2022 at 3:26
  • \$\begingroup\$ @97.100.97.109 And looks like it is similar to this answer. codegolf.stackexchange.com/a/142108/44718 so you can go up vote that one. \$\endgroup\$ Commented Sep 27, 2022 at 3:27
2
\$\begingroup\$

Pari/GP, 5 bytes

sigma

Try it online!

answered Sep 8, 2017 at 5:37
\$\endgroup\$
2
\$\begingroup\$

J, 23 bytes

[:+/](([:=&0]|[)#])1+i.

Try it online!

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.

answered Sep 8, 2017 at 6:10
\$\endgroup\$
3
  • 2
    \$\begingroup\$ This for 13 bytes should be equivalent: +1#.i.*0=i.|] \$\endgroup\$ Commented Sep 8, 2017 at 6:22
  • \$\begingroup\$ @miles, that is really nice. This part is i.|] is a great improvement on my approach. I don't fully understand this part though: +1#.i. -- could you explain it? \$\endgroup\$ Commented Sep 8, 2017 at 6:27
  • 2
    \$\begingroup\$ 1#. is base 1 conversion, which is equivalent to +/"1. First i.|] to get the remainders, then 0= to find the ones equal to 0 (the divisors), then i.* to zero out the non-divisors in the range, then sum using 1#., then add + itself since i. is an exclusive range. \$\endgroup\$ Commented Sep 8, 2017 at 6:31
2
\$\begingroup\$

Pyth, 6 bytes

s*M{yP

Try it here!

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.

answered Sep 8, 2017 at 7:13
\$\endgroup\$
2
\$\begingroup\$

Java (OpenJDK 8), (削除) 53 (削除ここまで) 51 bytes

n->{int s=0,i=0;for(;i++<n;)s+=n%i<1?i:0;return s;}

Try it online!

answered Sep 8, 2017 at 7:55
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @corsiKa Only class fields. In a local scope they're unitialized. \$\endgroup\$ Commented Sep 9, 2017 at 9:03
2
\$\begingroup\$

Jelly, 2 bytes

Æs

Try it online!

Built-in that does exactly as wanted.

answered Sep 8, 2017 at 0:37
\$\endgroup\$
4
  • \$\begingroup\$ Jelly seems so beautiful! Where can i find some stuff to study about it? \$\endgroup\$ Commented Sep 8, 2017 at 0:41
  • \$\begingroup\$ @KevinHalley You can check out the Tutorial page on Github, or you can visit the official Jelly chatroom once you have 20 reputation, or the Jelly training chatroom, also at 20 reputation, which you'll need to request permission to train in \$\endgroup\$ Commented Sep 8, 2017 at 0:42
  • 4
    \$\begingroup\$ I desperately want to upvote this because you suggested not upvoting trivial solutions. \$\endgroup\$ Commented Sep 8, 2017 at 6:01
  • \$\begingroup\$ @CodyGray Yeah, maybe there's no point putting that there :P \$\endgroup\$ Commented Sep 8, 2017 at 12:00
2
\$\begingroup\$

Haskell, 30 bytes

f n=sum[i|i<-[1..n],n`mod`i<1]

Try it online!

answered Sep 8, 2017 at 13:41
\$\endgroup\$
2
\$\begingroup\$

MATL, 6 bytes

t:\~fs

Try it online!

-4 bytes thanks to @LuisMendo

10 bytes

My previous solution using a loop

:"G@\~@*vs

Try it online!

3 bytes

Using built-in

Z\s

Try it online!

answered Sep 8, 2017 at 10:25
\$\endgroup\$
0
2
\$\begingroup\$

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))

answered Sep 8, 2017 at 0:58
\$\endgroup\$
0
2
\$\begingroup\$

Brain-Flak, 96 bytes

((({})<>){<(([()]{})){<>(({})(<()>))<>{(({})){({}[()])<>}{}}{}<>([{}()]({})){((<{}{}>))}}{}>{}})

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
answered Sep 15, 2017 at 0:55
\$\endgroup\$
2
  • \$\begingroup\$ Do you have an explanation? \$\endgroup\$ Commented Sep 19, 2017 at 2:33
  • \$\begingroup\$ @FunkyComputerMan I got one now! \$\endgroup\$ Commented Sep 21, 2017 at 19:56
2
\$\begingroup\$

R, (削除) 31 (削除ここまで) 26 bytes

function(N)(x=1:N)%*%!N%%x

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 but I only saw that after I thought of this.

R+ numbers, 14 bytes

numbers::Sigma

Try it online!

answered Sep 8, 2017 at 0:39
\$\endgroup\$
5
  • \$\begingroup\$ For the first one you can save 2 bytes with N=scan(); \$\endgroup\$ Commented Sep 8, 2017 at 12:07
  • \$\begingroup\$ @gstats yes, but then I should get +4 bytes per meta discussion. If you have a strong opinion you can weigh in on Jarko's answer but as nobody has suggested an alternative, that stands in my mind. \$\endgroup\$ Commented Sep 8, 2017 at 12:18
  • \$\begingroup\$ Shouldn't the second be numbers::Sigma(N)? Like this it outputs the source code of function Sigma. \$\endgroup\$ Commented Sep 8, 2017 at 15:58
  • \$\begingroup\$ @RuiBarradas a function is a perfectly good submission. to test it, you obviously have to call it as I do in the first submission. \$\endgroup\$ Commented Sep 8, 2017 at 15:59
  • \$\begingroup\$ Note: that anonymous functions in the later versions of R now can now use the syntax \(x) instead of function(x). So you can save quite a few characters there. \$\endgroup\$ Commented Sep 26, 2022 at 11:32
2
\$\begingroup\$

TI-Basic, 16 bytes

sum(seq(Inot(fPart(Ans/I)),I,1,Ans

Takes input in Ans. Output is stored in Ans and displayed.

answered Dec 8, 2021 at 0:35
\$\endgroup\$
2
\$\begingroup\$

Vyxal s, 1 byte

K

Try it Online!

K∑ flagless. K just gets the divisors of a number.

answered Dec 8, 2021 at 0:43
\$\endgroup\$
2
\$\begingroup\$

Husk, 2 bytes

ΣḊ

Try it online!

answered Jan 5, 2022 at 1:31
\$\endgroup\$
1
  • \$\begingroup\$ wow that's pretty good \$\endgroup\$ Commented Jan 5, 2022 at 2:51
2
\$\begingroup\$

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

Try it online!

answered Jan 5, 2022 at 2:49
\$\endgroup\$
3
  • \$\begingroup\$ {grep($_%%*,1..$_).sum} works for 23 \$\endgroup\$ Commented Jan 5, 2022 at 7:35
  • \$\begingroup\$ that that is very smart \$\endgroup\$ Commented Jan 5, 2022 at 16:35
  • \$\begingroup\$ you can move sum to the start to save one on the brackets, Try it online! \$\endgroup\$ Commented Jan 30, 2022 at 12:38
2
\$\begingroup\$

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))
answered Sep 17, 2017 at 19:48
\$\endgroup\$
2
  • 2
    \$\begingroup\$ For i=1To[A1]:s=s-([A1]mod i=0)*i:Next:?s is 41 bytes. The minus is because VBA seems to convert True to -1. \$\endgroup\$ Commented Dec 8, 2021 at 2:27
  • \$\begingroup\$ @Axuary - thanks for having me take a look at this one again - It was fun to make the worksheet formula solution for this \$\endgroup\$ Commented Jan 5, 2022 at 1:23
2
\$\begingroup\$

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!

-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

Try it online!

-7 bytes thanks to DLosc.

answered Feb 24, 2022 at 21:47
\$\endgroup\$
2
  • \$\begingroup\$ @DLosc thanks! I should probably rename D to M since now it's just a mod function. \$\endgroup\$ Commented Feb 28, 2022 at 20:11
  • \$\begingroup\$ Huh--I didn't notice that, lol. \$\endgroup\$ Commented Feb 28, 2022 at 23:27
2
\$\begingroup\$

APL(Dyalog Unicode), (削除) (削除ここまで)9 bytes SBCS

+/∘⍸0=⍳|⊢

Try it on APLgolf!

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
answered May 10 at 0:27
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 31 bytes

f=(n,i=n)=>i&&!(n%i)*i+f(n,i-1)
answered Sep 8, 2017 at 1:42
\$\endgroup\$
1
\$\begingroup\$

Bash + GNU utilities, 36

bc<<<`seq -f"n=%g;a+=n*!1ドル%%n;" 1ドル`a

Try it online.


Pure Bash, 41

for((;++i<=1ドル;a+=1ドル%i?0:i))
{
:
}
echo $a

Try it online.

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\\\))]
answered Sep 8, 2017 at 5:21
\$\endgroup\$
1
\$\begingroup\$

Python 2, 41 bytes

f=lambda n,i=1:i<=n and(n%i<1)*i+f(n,i+1)

Try it online!

answered Sep 8, 2017 at 5:43
\$\endgroup\$
1
2 3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.