34
\$\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\$

75 Answers 75

10
\$\begingroup\$

Haskell, 22 bytes

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

Try it online!

Explanation

Yes.

\$\endgroup\$
1
  • 7
    \$\begingroup\$ Great explanation \$\endgroup\$ Dec 15, 2017 at 3:32
8
\$\begingroup\$

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!

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

05AB1E, 3 bytes

L%O

Try it online!

\$\endgroup\$
7
\$\begingroup\$

Ruby, 28 27 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!

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

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!

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

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

R, 20 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Ohm v2, 4 bytes

D@%Σ

Try it online!

\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog), 5 bytes

+/⍳|⊢

Try it online!

How?

Monadic train -

+/ - sum

- n

| - vectorized modulo

- the range of n

\$\endgroup\$
4
\$\begingroup\$

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)

\$\endgroup\$
4
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – DJMcMayhem
    Dec 19, 2017 at 8:27
  • \$\begingroup\$ Oh wow! I didn't even think of that. Thanks \$\endgroup\$
    – Max00355
    Dec 19, 2017 at 19:26
  • 1
    \$\begingroup\$ Welcome to PPCG! You can use a list comprehension instead of map for 38 bytes: Try it online! \$\endgroup\$
    – Mr. Xcoder
    Dec 19, 2017 at 19:40
  • \$\begingroup\$ 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. \$\endgroup\$
    – Max00355
    Dec 19, 2017 at 19:52
4
\$\begingroup\$

Rust, 27 characters

|n|(1..n).map(|i|n%i).sum()

Based on Should rust anonymous functions fully specify parameter types? I assume that return type inference is also fine.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Julia 1.0, 16 bytes

~x=sum(x.%(1:x))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

Python 3, 37 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 4 answers in 10 minutes? Wow. \$\endgroup\$
    – Nissa
    Dec 14, 2017 at 1:31
  • 2
    \$\begingroup\$ Must be some very gifted programmers. ;) \$\endgroup\$
    – Neil
    Dec 14, 2017 at 1:32
3
\$\begingroup\$

Charcoal, 9 bytes

IΣEN﹪Iθ⊕ι

Try it online!

Link is to the verbose version of the code:

Print(Cast(Sum(Map(InputNumber(),Modulo(Cast(q),++(i))))));
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Java (OpenJDK 8), 45 bytes

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

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 For using the goes to (-->) operator. \$\endgroup\$
    – raznagul
    Dec 15, 2017 at 12:07
3
\$\begingroup\$

Mathematica, 18 bytes

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

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ same bytes Tr[#~Mod~Range@#]& \$\endgroup\$
    – ZaMoC
    Dec 14, 2017 at 11:53
3
\$\begingroup\$

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

4, 67 bytes

4 doesn't have any modulo built in.

3.79960101002029980200300023049903204040310499040989804102020195984

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

Vyxal, 3 bytes

ɽ%∑

Try it Online!

Vyxal s, 2 bytes

ɽ%

Try it Online!

\$\endgroup\$
2
\$\begingroup\$

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

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!

\$\endgroup\$
1
  • \$\begingroup\$ Oh actually I found a different 5 byter than you, didn't read yours correctly \$\endgroup\$
    – Dave
    Dec 14, 2017 at 2:59
2
\$\begingroup\$

Ruby, 23 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Julia 0.4, 15 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Add++, 14 bytes

L,RAdx$p@BcB%s

Try it online!

How it works

L,   - Create a lambda function.
     - Example argument:     [7]
  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 = [8]
\$\endgroup\$
2
\$\begingroup\$

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

PHP, 61 bytes

-2 bytes for removing the closing tag

<?php $z=fgets(STDIN);for($y=1;$y<$z;$y++){$x+=$z%$y;}echo$x;

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Japt -mx, 3 bytes

N%U

Try it online!

\$\endgroup\$

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.