27
\$\begingroup\$

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!

\$\endgroup\$

53 Answers 53

1 2
1
\$\begingroup\$

Neim, 3 bytes

Try It Online!

𝐈𝕄𝐬

Explanation:

𝐈      # Inclusive range
 𝕄    # Modulo each element with input
   𝐬   # Sum the list
\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 10 bytes

+/(⍳n)|n←⎕

Prompts for screen input.

\$\endgroup\$
1
\$\begingroup\$

Pari/GP, 17 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Although we already had the modulo challenge... this one seems different. +1. Explanation? \$\endgroup\$ – user202729 Dec 16 '17 at 3:49
1
\$\begingroup\$

Clean, 39 bytes

Literally just Bruce Forte's Haskell answer, but in Clean.

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

Try it online!

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

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.

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

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!

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

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

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

Gaia, 8 bytes

@:…)¦%¦Σ

Try it online!

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

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!

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

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.

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

Perl 6, 21 16 bytes

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

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

Try it online!

-5 bytes (!) thanks to Brad Gilbert.

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

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)

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

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

Common Lisp, 50 47 bytes

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

Try it online!

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

Befunge-93, 36 bytes

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

Try It Online

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

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

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

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

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.

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

F#, 35 bytes

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

Try it online!

Pretty straight-forward.

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

Ahead, 21 bytes

I&l@O+K<
~>td-! ntd%~

Try it online!

\$\endgroup\$
1 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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