Modulus Summation

I call this sequence "the Jesus sequence", because it is the sum of mod.</pun>

For this sequence, you take all the positive integers m less than the input n, and take the sum of n modulo each m. In other words:

$$a_n = \sum_{m=1}^{n-1}{n\bmod m}$$

For example, take the term 14:

14 % 1 = 0
14 % 2 = 0
14 % 3 = 2
14 % 4 = 2
14 % 5 = 4
14 % 6 = 2
14 % 7 = 0
14 % 8 = 6
14 % 9 = 5
14 % 10 = 4
14 % 11 = 3
14 % 12 = 2
14 % 13 = 1
0+0+2+2+4+2+0+6+5+4+3+2+1=31

Your goal here is to write a function that implements this sequence. You should take the sequence term (this will be a positive integer from 1 to 231) as the only input, and output the value of that term. This is OEIS A004125.

As always, standard loopholes apply and the shortest answer in bytes wins!

05AB1E, 3 bytes

L%O

Try it online!

f x=sum$mod x<$>[1..x]

Try it online!

Explanation

Yes.

• Great explanation – Esolanging Fruit Dec 15 '17 at 3:32

Ruby, 2827 23 bytes

-4 bytes thanks to @daniero.

->n{(1..n).sum{|i|n%i}}

Try it online!

Ruby, 28 bytes

f=->n{n>($.+=1)?n%$.+f[n]:0}

Try it online!

Funky, 25 bytes

n=>fors=~-i=1i<n)s+=n%i++

Just the Naïve answer, seems to work.

Try it online!

Desmos, 25 bytes.

f(x)=\sum_{n=1}^xmod(x,n)

Paste into Desmos, then run it by calling f.

When pasted into Desmos, the latex looks like this The graph however looks like Although it looks random and all over the place, that's the result of only supporting integers.

RProgN 2, 9 bytes

x=x³x\%S+

Explained

x=x³x\%S+
x=          # Store the input in "x"
x         # Push the input to the stack.
³x\%     # Define a function which gets n%x
S    # Create a stack from "x" with the previous function. Thus this gets the range from (1,x), and runs (i%x) on each element.
+   # Get the sum of this stack.

Try it online!

ReRegex, 71 bytes

#import math
(_*)_a$/d<$1_>b$1a/(\d+)b/((?#input)%$1)+/\+a//u<#input
>a

Try it online!

ARBLE, 19 bytes

sum(range(1,a)|a%i)

Try it online!

MaybeLater, 56 bytes

whenf is*{n=0whenx is*{ifx>0{n=n+f%x x--}elseprintn}x=f}

Try it online!

• Will the submissions to this challenge ever end? So far I've been getting a new one every 40 minutes :P – Nissa Dec 15 '17 at 3:26
• @StephenLeppik Oh I've still got more coming, don't worry. – ATaco Dec 15 '17 at 3:33
• @StephenLeppik I'd rather not, because they're of various quality across various languages. – ATaco Dec 15 '17 at 3:42
• @StephenLeppik I've combined them for you, begrudgingly. – ATaco Dec 15 '17 at 3:52
• Please don't do this. Separate languages -- even separate approaches -- should go in separate answers. – Dennis Dec 18 '17 at 4:48

Jelly, 3 bytes

%RS

Explanation

%RS
R   Range(input)  [1...n]
%    Input (implicit) modulo [1...n]->[n%1,n%2...n%n]
S  Sum of the above

Try it online!

MATL, 4 bytes

t:\s

Try it online!

Explanation:

t      % Duplicate input. Stack: {n, n}
:     % Range 1...n. Stack: {n, [1...n]}
\    % Modulo. Stack: {[0,0,2,2,4,...]}
s   % Sum. Implicitly display result.

D@%Σ

Try it online!

R, 20 bytes

sum((n=scan())%%1:n)

Try it online!

Python 2, 44 bytes

lambda n:sum(map(lambda x:x%(n-x),range(n)))

Try it online!

EDIT: Changed range(0,n) to range(n)

• Hello, and welcome to the site! range implicitly takes a first argument of 0, so you could shorten this by two bytes by doing range(n) instead. – DJMcMayhem Dec 19 '17 at 8:27
• Oh wow! I didn't even think of that. Thanks – Max00355 Dec 19 '17 at 19:26
• Welcome to PPCG! You can use a list comprehension instead of map for 38 bytes: Try it online! – Mr. Xcoder Dec 19 '17 at 19:40
• You are right, but that was used in Neil's answer, so I wasn't sure if copying it would have been the best thing. Unless I am missing something here of course. I wanted to post the alternative, even if it was a bit longer. – Max00355 Dec 19 '17 at 19:52

JavaScript (ES6), 26 bytes

f=(n,k=n)=>n&&k%n+f(n-1,k)

Demo

f=(n,k=n)=>n&&k%n+f(n-1,k)

for(n = 1; n < 30; n++) {
console.log('a(' + n + ') = ' + f(n))
}

Python 3, 37 bytes

lambda a:sum(a%k for k in range(1,a))

Try it online!

• 4 answers in 10 minutes? Wow. – Nissa Dec 14 '17 at 1:31
• Must be some very gifted programmers. ;) – Neil Dec 14 '17 at 1:32

Charcoal, 9 bytes

ＩΣＥＮ﹪Ｉθ⊕ι

Try it online!

Link is to the verbose version of the code:

Print(Cast(Sum(Map(InputNumber(),Modulo(Cast(q),++(i))))));

Standard ML (MLton), 53 51 bytes

fn& =>let fun f 1a=a|f%a=f(% -1)(a+ &mod%)in f&0end

Try it online!

Ungolfed:

fn n =>
let fun f 1 a = a
| f x a = f (x-1) (a + n mod x)
in
f n 0
end

Previous 53 byte version:

fn n=>foldl op+0(List.tabulate(n-1,fn i=>n mod(i+1)))

Try it online!

Explanation:

List.tabulate takes an integer x and a function f and generates the list [f 0, f 1, ..., f(x-1)]. Given some number n, we call List.tabulate with n-1 and the function fn i=>n mod(i+1) to avoid dividing by zero. The resulting list is summed with foldl op+0.

Java (OpenJDK 8), 45 bytes

n->{int m=n,s=0;for(;m-->1;)s+=n%m;return s;}

Try it online!

• +1 For using the goes to (-->) operator. – raznagul Dec 15 '17 at 12:07

Mathematica, 18 bytes

#~Mod~i~Sum~{i,#}&

Try it online!

• same bytes Tr[#~Mod~Range@#]& – J42161217 Dec 14 '17 at 11:53

APL (Dyalog), 5 bytes

+/⍳|⊢

Try it online!

How?

+/ - sum

- n

| - vectorized modulo

- the range of n

Japt, 6 5 bytes

Saved 1 byte thanks to @Shaggy

Æ%XÃx

Test it online!

How it works

Implicit: U = input number
Æ        Create the range [0, U),
%XÃ       mapping each item X to U%X. U%0 gives NaN.
x    Sum, ignoring non-numbers.
Implicit: output result of last expression

05AB1E, 6 bytes

ÎGIN%+

Try it online!

My first 05AB1E program ;)

Btw I got two 39s, 1 for JS6 and 1 for python, but I was too late

Explanation:

ÎGIN%+
Î                      # Push 0, then input, stack = [(accumulator = 0), I]
G                     # For N in range(1, I), stack = [(accumulator)]
IN                   # Push input, then N, stack = [(accumulator), I, N]
%                  # Calculate I % N, stack = [(accumulator), I % N]
+                 # Add the result of modulus to accumulator

Ruby, 23 bytes

->n{(1..n).sum{|i|n%i}}

Try it online!

Julia 0.4, 15 bytes

x->sum(x%[1:x])

Try it online!

L,RAdx$p@BcB%s Try it online! How it works L, - Create a lambda function. - Example argument:  R - Range; STACK = [[1 2 3 4 5 6 7]] A - Argument; STACK = [[1 2 3 4 5 6 7] 7] d - Duplicate; STACK = [[1 2 3 4 5 6 7] 7 7] x - Repeat; STACK = [[1 2 3 4 5 6 7] 7 [7 7 7 7 7 7 7]]$p - Swap and pop; STACK = [[1 2 3 4 5 6 7] [7 7 7 7 7 7 7]]
@  - Reverse;      STACK = [[7 7 7 7 7 7 7] [1 2 3 4 5 6 7]]
Bc - Zip;          STACK = [[7 1] [7 2] [7 3] [7 4] [7 5] [7 6] [7 7]]
B% - Modulo each;  STACK = [0, 1, 1, 3, 2, 1, 0]
s  - Sum;          STACK = 

4, 67 bytes

4 doesn't have any modulo built in.

3.79960101002029980200300023049903204040310499040989804102020195984

Try it online!

Windows Batch (CMD), 63 bytes

@set s=0
@for /l %%i in (1,1,%1)do @set/as+=%1%%%%i
@echo %s%

Previous 64-byte version:

@set/ai=%2+1,s=%3+%1%%i
@if %i% neq %1 %0 %1 %i% %s%
@echo %s%

T-SQL, 80 79 bytes

-1 byte thanks to @MickyT

WITH c AS(SELECT @ i UNION ALL SELECT i-1FROM c WHERE i>1)SELECT SUM(@%i)FROM c

Receives input from an integer parameter named @, something like this:

DECLARE @ int = 14;

Uses a Common Table Expression to generate numbers from 1 to n. Then uses that cte to sum up the moduluses.

Note: a cte needs a ; between the previous statement and the cte. Most code I've seen puts the ; right before the declaration, but in this case I can save a byte by having it in the input statement (since technically my code by itself is the only statement).

Try it (SEDE)

The less "SQL-y" way is only 76 bytes. This time the input variable is @i instead of @ (saves one byte). This one just does a while loop.

DECLARE @ int=2,@o int=0WHILE @<@i BEGIN SELECT @o+=@i%@,@+=1 END PRINT @o

PHP, 61 bytes

-2 bytes for removing the closing tag

Try it online!

Pyth, 5 bytes

s%LQS

s%LQS - Full program, inputs N from stdin and prints sum to stdout
s     - output the sum of
%LQ  - the function (elem % N) mapped over
S - the inclusive range from 1..N

Try it online!

• Oh actually I found a different 5 byter than you, didn't read yours correctly – Dave Dec 14 '17 at 2:59