# 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!

# Neim, 3 bytes

Try It Online!

ššš¬


Explanation:

š      # Inclusive range
š    # Modulo each element with input
š¬   # Sum the list


# APL+WIN, 10 bytes

+/(ā³n)|nāā


Prompts for screen input.

# Pari/GP, 17 bytes

n->sum(m=1,n,n%m)


Try it online!

Labyrinth - 45 Characters

?:}}""):={%={{+
;        "
@!{{{-{=:}}}::}


Basically, it sets up by taking the input of the number that we are modulo summing. It then starts at one, takes the modulo of that number, and checks if it has reached the target number yet. If it hasn't, take the modulo again and add it to the previous one.

(I left out a lot of stack manipulations if you hadn't guessed, and yes, it actually does output the sum of the modulus up to that number)

# Brain-Flak, 70 62 bytes

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


Try it online!

It turns out that the straightforward approach is better. Also, keeping a copy of n on the third stack instead of the first stack saves bytes.

(([{}])<>)                                                      Initialize both stacks with -n
{                                              }{}   For all k from n down to 1:
<>(({})<                      >)                   Store -n on third stack
<>{(({})){({}())<>}{}}                     Main part of standard modulus program
(except that the arguments are negative)
<>
<                                  >                Ignore evaluation of -n
{}[({}())]      Recover n mod k, and decrement k for the next iteration
(                                                  )  Sum over all iterations and push

• Although we already had the modulo challenge... this one seems different. +1. Explanation? – user202729 Dec 16 '17 at 3:49

# Clean, 39 bytes

import StdEnv
@x=sum(map((rem)x)[1..x])


Try it online!

• Well there's pretty much only one (short) way to solve this task.. Btw. using Haskell would save you 17 bytes ;P – ბიმო Dec 18 '17 at 22:52

# Ruby, 23 bytes

->n{eval [*0..n]*"+n%"}


Try it online!

Evaluates a string like 0+n%1+n%2+n%3+n%4+n%5 for n=5.

# Labyrinth, 38 bytes

?_"):}}:{%={
@ ;        {
!{{=-{:}}:}+


Try it online!

### How it works

Stack notation is a b ... c | d e ... f where a b ... c part is the main stack, d e ... f is auxiliary, and c and d are the top of two stacks. I found this notation useful in developing and explaining the answer.

        Start at the top left
?_"     Push input and a 0, going straight through the junction
n i=0 | sum=0
)       Increment i
:}}     n | i i sum
:{%=    n i | n%i sum
{{+}    n i | sum'=sum+n%i
:}}:{-  n n-i | i sum'
={      n i n-i | sum'
;       If n-i is nonzero, turn right and discard n-i, continuing the loop
{!@     Otherwise go straight, print sum and terminate

• Starting from n%n and counting down to zero works better for 24 bytes. I suspect the loop can still be golfed – Jo King Sep 30 at 10:27

# C, 43 bytes

s,m;f(n){for(s=m=0;m++<n;s+=n%m);return s;}


Try it online!

# C (gcc), 38 bytes

s,m;f(n){for(s=m=0;m++<n;s+=n%m);s=s;}


Try it online!

• That's my best, too: a;i;f(n){a=0;for(i=n;--i;)a+=n%i;return a;} – Toby Speight Dec 19 '17 at 18:03

# Pip, 7 bytes

$+a%\,a  Try it online!  a is 1st cmdline arg \,a Inclusive range from 1 through a a% Take a mod each of those numbers (a%a is 0, so doesn't affect the sum)$+       Fold on +
Output (implicit)


# MATLAB, 19 bytes

@(n)sum(mod(n,1:n))


Very straightforward, mod calculates n%x for each natural number below n. and sum just sums them all. Test:

@(n)sum(mod(n,1:n))
ans(14)
ans =

@(n)sum(mod(n,1:n))
ans =
31.0000e+000


# Gaia, 8 bytes

@:ā¦)Ā¦%Ā¦Ī£


Try it online!

# J, 9 bytes

-~1#.i.|]


## How it works

        ] - the argument
|  - mod
i.   - list from 0 to argument - 1
1#.     - sum (base 1 conversion)
-~        - subtract the argument (to get rid of the n mod 0)


Try it online!

## Clojure, 38 bytes

#(apply +(for[i(range 1 %)](mod % i)))


Well this is boring, I was hoping #(apply +(map mod(repeat %)(range 1 %))) would have been shorter as mod gets rarely used in map context.

# Perl 6, 21 16 bytes

{sum $_ Ā«%Ā«(1..$_)}

{sum $_ X%1..$_}


Try it online!

-5 bytes (!) thanks to Brad Gilbert.

• This challenge is getting way more submissions than I anticipated. – Nissa Dec 14 '17 at 18:07
• @StephenLeppik It's because it's easy... xD – Sanchises Dec 15 '17 at 13:37
• {sum $_ X%1..$_} – Brad Gilbert b2gills Dec 15 '17 at 17:17

# J, 14 Bytes

M=:+/@:|~}.@i.


### How it works:

M=:                 | Define the verb M
i.      | Get integers in the range [0, input)
}.@        | Remove the leading 0
|~           | Take the original input mod each of these integers, making a new array
+/@:             | Sum this array


### Example:

    M 14
31
M"0 >:i.20    NB. Apply M to each of the integers from 1-20 inclusive
0 0 1 1 4 3 8 8 12 13 22 17 28 31 36 36 51 47 64 61


# Common Lisp, 50 47 bytes

(lambda(n)(loop as i from 1 to n sum(mod n i)))


Try it online!

# Befunge-93, 36 bytes

p&:10p1>-#1:_\$v_g.@
:p00+g00%\g01<^


Try It Online

# Pushy, 13 bytes

&1{:2d%vh;OS#


Try it online!

&1{                 \ Make the stack [n, 1, n]
:     ;          \ n times do (this consumes last n):
2d              \    Copy the last two items
%v            \    Get remainder and put result on stack
h           \    Increment divisor
OS        \ Sum the second stack
#       \ Output the result


# SNOBOL4 (CSNOBOL4), 69 68 bytes

	N =INPUT
A	I =I + 1
S =LT(I,N) S + REMDR(N,I)	:S(A)
OUTPUT =S
END


Try it online!

	N =INPUT				;* read in input
A	I =I + 1				;* I is initialized to 0 so this increments it to 1
S =LT(I,N) S + REMDR(N,I)	 :S(A)	;* similarly with S, add to the remainder
LT(I,N) :S(A)				;* on success (I < N), goto A
OUTPUT =S				;* output the sum
END						;* terminate the program


# Forth (gforth), 35 bytes

: f 0 over 1 do over i mod + loop ;


Try it online!

### Explanation

Loops over all numbers from 1 to n-1 and adds n%i to the sum

### Code Explanation

: f            \ start a new word definition
0 over       \ add a 0 to the stack and place a copy of n above it
1 do         \ start a counted loop from 1 to n-1
over i     \ copy n to the the top of the stack and place the loop index above it
mod +      \ take n%i and add it to the sum
loop         \ end the counted loop
;              \ end the word definition



# dc, 31 bytes

[dz%rdz<B]sBd1<B0*0[+z1<C]dsCxp


Try it online!

Assume our input is the only value on the stack. Macro B duplicates top of stack, calculates stack depth, and performs modulo. Runs until stack depth is equal to the input. This works for numbers greater than 1, so we don't run B automatically, but instead only if it's greater than 1 (d1<B). B leaves the input value on top-of-stack, so we multiply by 0. Since 0 is the desired result for an input of 1, this is fine for 1 as well. However, 1 doesn't leave us enough items on the stack to do the initial summation in macro C, so we put another 0 on the stack just in case. Macro C simply adds the two values on top of the stack, and keeps going until the stack only has a single value. Finally, we print.

## F#, 35 bytes

let s v=Seq.sumBy(fun x->v%x){1..v}


Try it online!

Pretty straight-forward.

I&l@O+K<