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

\$\endgroup\$
7
  • 6
    \$\begingroup\$ @H.PWiz I think he means "the divisors of a number N" \$\endgroup\$
    – benzene
    Commented Sep 8, 2017 at 0:18
  • \$\begingroup\$ I think you mean sum of divisors, aka, the sigma function? \$\endgroup\$
    – Stephen
    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\$
    – Stephen
    Commented Sep 8, 2017 at 0:21
  • \$\begingroup\$ @Stephen That seems like a trivial change to me \$\endgroup\$
    – H.PWiz
    Commented Sep 8, 2017 at 0:21

67 Answers 67

1 2
3
0
\$\begingroup\$

Ruby, 29 bytes

->x{(1..x).sum{|r|x%r>0?0:r}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Recursiva, 20 18 bytes

smBa++'%'Va'a:0!a'

Try it online!

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
\$\endgroup\$
0
\$\begingroup\$

Clojure, 53 bytes

#(apply +(for[i(range 1(inc %)):when(=(mod % i)0)]i))
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 27 bytes

->x{(1..x).sum{1[x%_1]*_1}}

Attempt This Online!

Abuses Ruby's nth bit operator.

\$\endgroup\$
0
\$\begingroup\$

MathGolf, 2 bytes

─Σ

One of many 2-bytes answers in a code-golf language.

Try it online.

Explanation:

─   # Get a list of divisors of the (implicit) input-integer
 Σ  # Sum them together
    # (after which the entire stack is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

cQuents, 3 bytes

Uz$

Try it online!

Explanation

     implicit mode sequence - print nth term in sequence given input n
U    sum (                           )
 z         divisors (              )
  $                   current term
\$\endgroup\$
1 2
3

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.