模总和


27

我将此序列称为“耶稣序列”,因为它是mod总和。</ pun>

对于这个序列,你把所有的正整数中号小于输入ñ,走的总和ñ模每。换一种说法:

an=m=1n1nmodm

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!

Answers:





6

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!


向这一挑战提交的作品会永远结束吗?到目前为止,我每40分钟就会得到一个新的:P
妮莎(Nissa)

@StephenLeppik哦,我还有更多来,别担心。
ATaco

@StephenLeppik I'd rather not, because they're of various quality across various languages.
ATaco

@StephenLeppik I've combined them for you, begrudgingly.
ATaco

4
Please don't do this. Separate languages -- even separate approaches -- should go in separate answers.
Dennis


5

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.



4

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)


2
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

哇!我什至没有想到这一点。谢谢
Max00355

1
Welcome to PPCG! You can use a list comprehension instead of map for 38 bytes: Try it online!
Mr. Xcoder

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

3

JavaScript (ES6), 26 bytes

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

Demo




3

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.





3

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

2

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



2

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]

2

4, 67 bytes

4 doesn't have any modulo built in.

3.79960101002029980200300023049903204040310499040989804102020195984

Try it online!


2

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%

2

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




1

Husk, 5 bytes

ΣṠM%ḣ

Try it online!

Explanation

ΣṠM%ḣ  -- implicit input x, example: 5
 ṠM%   -- map (mod x) over the following..
    ḣ  -- ..the range [1..x]: [5%1,5%2,5%3,5%4,5%5] == [0,1,2,1,0]
Σ      -- sum: 0+1+2+1+0 == 4


1

Pyth,5个字节

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

在线尝试!


Oh actually I found a different 5 byter than you, didn't read yours correctly
Dave
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.