| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | Vyxal 3 | 250808T124048Z | Themooni |
| 044 | Setanta | 250807T195146Z | bb94 |
| 028 | AWK | 250807T165915Z | xrs |
| 012 | K ngn/k | 231103T115939Z | coltim |
| 225 | Malbolge Unshackled | 231103T112714Z | Kamila S |
| 008 | APLDyalog Unicode | 231019T091833Z | Kamila S |
| 007 | Uiua | 231016T004011Z | chunes |
| 021 | Arturo | 230612T192927Z | chunes |
| 045 | Trilangle | 230612T191212Z | Bbrk24 |
| 091 | Rockstar | 230612T101000Z | Shaggy |
| 002 | Thunno 2 S | 230611T165149Z | The Thon |
| 020 | Mathematica | 221009T200642Z | hakr14 |
| 052 | Nim | 221009T195738Z | naffetS |
| 016 | Julia 1.0 | 221009T193832Z | Ashlin H |
| nan | 221007T144911Z | bigyihsu | |
| 016 | K ngn/k | 221007T141842Z | oeuf |
| 027 | Rust | 221007T012337Z | manatwor |
| 024 | Juby | 221006T234317Z | Jordan |
| 030 | PowerShell Core | 210621T230525Z | Julian |
| 013 | Ly | 210619T234503Z | cnamejj |
| 030 | Knight | 210620T012626Z | EasyasPi |
| 033 | Factor + math.unicode | 210620T020958Z | chunes |
| 003 | Vyxal | 210619T223825Z | Undersla |
| 328 | MMIX | 210619T221425Z | NoLonger |
| 038 | Labyrinth | 200924T063312Z | Bubbler |
| 023 | Ruby | 180821T094755Z | lynn |
| 009 | Brachylog | 180821T092933Z | Kroppeb |
| 021 | Ahead | 180821T054801Z | snail_ |
| 035 | F# | 180820T201834Z | Ciaran_M |
| 031 | dc | 180820T183932Z | brhfl |
| 003 | Japt mx | 180820T180633Z | Luis fel |
| 035 | Forth gforth | 180820T164808Z | reffu |
| 061 | PHP | 171217T205133Z | NK1406 |
| 023 | Ruby | 171214T014813Z | Jordan |
| 068 | SNOBOL4 CSNOBOL4 | 171214T203008Z | Giuseppe |
| 044 | Python 2 | 171219T080642Z | Max00355 |
| 079 | TSQL | 171214T175352Z | Brian J |
| 013 | Pushy | 171218T173200Z | FlipTack |
| 036 | Befunge93 | 171218T165024Z | Jo King |
| 047 | Common Lisp | 171218T153701Z | Renzo |
| 039 | Clean | 171216T073411Z | Οurous |
| 062 | BrainFlak | 171215T023900Z | Nitrodon |
| 014 | J | 171216T040437Z | Bolce Bu |
| 063 | Windows Batch CMD | 171214T092030Z | Neil |
| 045 | Labyrinth 45 Characters | 171215T232943Z | C Anders |
| 016 | Perl 6 | 171214T180449Z | Sean |
| 025 | Funky | 171215T032505Z | ATaco |
| 038 | Clojure | 171214T220722Z | NikoNyrh |
| 067 | 4 | 171214T213217Z | Uriel |
| 005 | Japt | 171214T020033Z | ETHprodu |
| 009 | J | 171214T085914Z | Galen Iv |
| 008 | Gaia | 171214T190839Z | Giuseppe |
| 019 | MATLAB | 171214T175030Z | brainkz |
| 014 | Add++ | 171214T173323Z | caird co |
| 015 | Julia 0.4 | 171214T162039Z | Lescurel |
| 017 | Pari/GP | 171214T132158Z | alephalp |
| 005 | APL Dyalog | 171214T123940Z | Uriel |
| 010 | APL+WIN | 171214T123423Z | Graham |
| 018 | Mathematica | 171214T113031Z | totallyh |
| 045 | Java OpenJDK 8 | 171214T110246Z | Olivier |
| 003 | Neim | 171214T103231Z | LiefdeWe |
| 023 | Ruby | 171214T094912Z | G B |
| 007 | Pip | 171214T092616Z | DLosc |
| 020 | R | 171214T092505Z | plannapu |
| 051 | Standard ML MLton | 171214T085437Z | Laikoni |
| 004 | MATL | 171214T081646Z | Sanchise |
| 004 | Ohm v2 | 171214T080633Z | Cinaski |
| 009 | Charcoal | 171214T074540Z | Charlie |
| 003 | 05AB1E | 171214T052701Z | Mr. Xcod |
| 005 | Pyth | 171214T025548Z | Dave |
| 022 | Haskell | 171214T030308Z | ბიმო |
| nan | Perl 5 | 171214T030018Z | Xcali |
| 006 | 05AB1E | 171214T021452Z | Shieru A |
| 005 | Husk | 171214T015643Z | ბიმო |
| 037 | Python 3 | 171214T013111Z | Neil |
| 026 | JavaScript ES6 | 171214T013026Z | Arnauld |
| 043 | C | 171214T013005Z | Steadybo |
| 003 | Jelly | 171214T012551Z | dylnan |
Vyxal 3, 3 bytes
ɾ%∑
ɾ%∑
ɾ # range 1..implicit input
% # vectorized mod implicit input
∑ # sum
💎
Created with the help of Luminespire.
<script type="vyxal3">
ɾ%∑
</script>
<script>
args=["1", "2", "3", "4", "5", "6", "7", "8", "9"]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
K (ngn/k), 13 12 bytes
-1 byte from @ovs' improvement
+/1_!'/|1!:\
|1!:\generate a two-item list containing(0..n; n)!'/modnby each of0..n1_drop the first result, corresponding ton % 0+/take the sum and (implicitly) return
Malbolge Unshackled, 1,828,225 bytes.
Available on GitHub as StackExchange does not allow posting answers this long.
Use as, having obtained fast20 or any Malbolge Unshackled interpreter:
$ echo -ne "\xFF" | time ./fast20 150563.mb | xxd
00000000: c3
Trilangle, 45 bytes
<'?0'1...Lj2'<.zj2'.......L-)S+%,7#..@!,,,/^S
Try it on the online interpreter!
TODO: add colorized explanation
<'?0'1...Lj2'<.zj2'.......L-)S+%,7#..@!,,,/^S
? Get number from stdin
0' Start sum at 0
' 1 Start counter at 1
j2' zj2' L- 7 Compare the counter and the input
@!,,,/ If they're the same, print the sum and exit
)S+%, S Otherwise, add one iteration and increment the counter
< . # ^ Loop
Rockstar, 91 bytes
listen to N
D's 0
O's 0
while N-D
let D be+1
let M be N/D
turn M down
let O be+N-M*D
say O
Try it (Code will need to be pasted in)
Thunno 2 S, 2 bytes
R%
Explanation
R% # Implicit input.
R # Range. Push [1..input].
% # Mod. Vectorises.
# S flag sums.
# Implicit output.
K (ngn/k), 16 bytes (Non-competitive)
{+/{x!g}'1+!g-1}
Non-competitive, because required the use of a global variable called g to get input. The reason is because lambda functions in K cannot access variables of another function if they are in that function.
Explanation:
{+/{x!g}'1+!g-1} Main function. Takes g as input (g=14)
! Generate a range from 0 to
g-1 g - 1 (exclusive) (g-1=13=>range=0..12)
1+ + 1 for each element (range=1..13)
' For each element
{ } Execute a function that returns
x!g g mod x (x = element)
+/ Sum
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.
Ly, 15 13 bytes
ns,[:lf%f,]&+
Thanks LyricLy for the tip that dropped 2 chars.
This generates the modulo numbers on a stack, then sums them and relies on the default action to print the stack to show the answer.
ns, # Read the N req from STDIN, stash it away, then decrement
[ ,] # Loop to decrement the top of stack and exit then that's 0
:l # Duplicate the top of stack, restore N from backup cell
f%f # Flip top two stack enrtries, do modulo math, flip again
&+ # Sum the stack
# Ends with one entry on the stack, which prints as a number
Knight, 30 bytes
;=x+=s=y 0P;W>x=y+yT=s+s%x yOs
Pretty straightforward.
# init x with input converted to number,
# and sum and y to 0
; = x + (= sum = y 0) PROMPT
# for 1..x
; WHILE (> x (= y + y 1))
# sum += x % y
: = sum + s (% x y)
: OUTPUT sum
Factor + math.unicode, 33 bytes
[ dup [1,b) [ mod ] with map Σ ]
dupDuplicate the input.Stack: (e.g.)
14 14[1,b)Make a range from1inclusive to14exclusive.Stack:
14 { 1 2 ... 13 }[ mod ] with mapDo14 1 mod,14 2 mod,14 3 mod, etc. and collect the results in a sequence the same length as the range.Stack:
{ 0 0 2 2 4 2 0 6 5 4 3 2 1 }ΣSum.Stack:
31
MMIX, 32 bytes (8 instrs)
00000000: c1020000 e3010000 1cff0002 25020201 Ḋ£¡¡ẉ¢¡¡÷”¡£%££¢
00000010: feff0006 200101ff 5b02fffc f8020000 “”¡© ¢¢”[£”‘ẏ£¡¡
modsum SET $2,$0 // i = n
SET $1,0 // s = 0
0H DIV $255,$0,$2
SUB $2,$2,1
GET $255,rR
ADD $1,$1,$255 // loop: s += n % i--
PBNZ $2,0B // if(i) goto loop
POP 2,0 // return [s,n]
Labyrinth, 38 bytes
?_"):}}:{%={
@ ; {
!{{=-{:}}:}+
Slight improvement to existing answer.
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
dc, 31 bytes
[dz%rdz<B]sBd1<B0*0[+z1<C]dsCxp
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.
Forth (gforth), 35 bytes
: f 0 over 1 do over i mod + loop ;
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
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;
Ruby, 28 27 23 bytes
-4 bytes thanks to @daniero.
->n{(1..n).sum{|i|n%i}}
Ruby, 28 bytes
f=->n{n>($.+=1)?n%$.+f[n]:0}
SNOBOL4 (CSNOBOL4), 69 68 bytes
N =INPUT
A I =I + 1
S =LT(I,N) S + REMDR(N,I) :S(A)
OUTPUT =S
END
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
Python 2, 44 bytes
lambda n:sum(map(lambda x:x%(n-x),range(n)))
EDIT: Changed range(0,n) to range(n)
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).
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
Pushy, 13 bytes
&1{:2d%vh;OS#
&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
Clean, 39 bytes
Literally just Bruce Forte's Haskell answer, but in Clean.
import StdEnv
@x=sum(map((rem)x)[1..x])
Brain-Flak, 70 62 bytes
(([{}])<>)({<<>(({})<<>{(({})){({}())<>}{}}>)<>>{}[({}())]}{})
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
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
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%
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)
Funky, 25 bytes
n=>fors=~-i=1i<n)s+=n%i++
Just the Naïve answer, seems to work.
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.
ReRegex, 71 bytes
#import math
(_*)_a$/d<$1_>b$1a/(\d+)b/((?#input)%$1)+/\+a//u<#input
>a
ARBLE, 19 bytes
sum(range(1,a)|a%i)
MaybeLater, 56 bytes
whenf is*{n=0whenx is*{ifx>0{n=n+f%x x--}elseprintn}x=f}
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.
4, 67 bytes
4 doesn't have any modulo built in.
3.79960101002029980200300023049903204040310499040989804102020195984
Japt, 6 5 bytes
Saved 1 byte thanks to @Shaggy
Æ%XÃx
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
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)
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
Add++, 14 bytes
L,RAdx$p@BcB%s
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]
APL (Dyalog), 5 bytes
+/⍳|⊢
How?
Monadic train -
+/ - sum
⊢ - n
| - vectorized modulo
⍳ - the range of n
APL+WIN, 10 bytes
+/(⍳n)|n←⎕
Prompts for screen input.
Neim, 3 bytes
𝐈𝕄𝐬
Explanation:
𝐈 # Inclusive range
𝕄 # Modulo each element with input
𝐬 # Sum the list
Pip, 7 bytes
$+a%\,a
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)
Standard ML (MLton), 53 51 bytes
fn& =>let fun f 1a=a|f%a=f(% -1)(a+ &mod%)in f&0end
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)))
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.
MATL, 4 bytes
t:\s
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.
Charcoal, 9 bytes
IΣEN﹪Iθ⊕ι
Link is to the verbose version of the code:
Print(Cast(Sum(Map(InputNumber(),Modulo(Cast(q),++(i))))));
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
05AB1E, 6 bytes
ÎGIN%+
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
Husk, 5 bytes
ΣṠM%ḣ
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
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))
}
C, 43 bytes
s,m;f(n){for(s=m=0;m++<n;s+=n%m);return s;}
C (gcc), 38 bytes
s,m;f(n){for(s=m=0;m++<n;s+=n%m);s=s;}
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