I'm honestly surprised that this hasn't been done already. If you can find an existing thread, by all means mark this as a duplicate or let me know.

Input

Your input is in the form of any positive integer greater than or equal to 1.

Output

You must output the sum of all integers between and including 1 and the number input.

Example

 In: 5
     1+2+3+4+5 = 15
Out: 15

OEIS A000217

share|improve this question
4  
Closely related – FryAmTheEggman 8 hours ago
    
@FryAmTheEggman Sorry - had a bit of a brain fart there. I see what you mean. – GarethPW 8 hours ago
    
I expected a bunch of very short answers (10 or 15 bytes) all either using the summation formula or a language summation feature. But DANG! A bunch of them at 2 bytes!? Is this now the "golf of code golfs," or have there been any others with 2-byte solutions (or what about 1 byte)? – Aaron 7 hours ago
    
@Aaron you got ninja'd by Husk, which was just posted with a 1 byte solution – Mayube 7 hours ago
    
@Mayube Wow.... I originally thought this was a silly question, "It's just going to be a bunch of different languages typing out the summation formula, or if it's built in then doing sum(x); why would someone bother writing such a silly so-called golf?" But now I don't know what to say. I have been schooled. I will +1 this question after all. – Aaron 6 hours ago

58 Answers 58

Pyth, 2 bytes

sS

Try it online! Implicit input. S is 1-indexed range, and s is the sum.

share|improve this answer
14  
Finally, Pyth(on) code sounds like a snake. – totallyhuman 8 hours ago
    
This is the perfect challenge for Pyth... – Mr. Xcoder 7 hours ago

Brain-Flak, 16 bytes

({({}[()])()}{})

Try it online!

This is one of the few things that brain-flak is really good at.

share|improve this answer

Jelly, 2 bytes

RS

Explanation

RS

    implicit input
 S  sum of the...
R   inclusive range [1..input]
    implicit output

Try it online!

Gauss sum, 3 bytes

‘×H

Explanation

‘×H

     implicit input
  H  half of the quantity of...
‘    input + 1...
 ×   times input
     implicit output
share|improve this answer
    
This also works in Anyfix :P (not on TIO) – HyperNeutrino 8 hours ago

Oasis, 3 bytes

n+0

Try it online!

How it works

n+0
  0    a(0)=0
n+     a(n)=n+a(n-1)
share|improve this answer

Haskell, 13 bytes

This is the shortest (I think):

f n=sum[1..n]

Try it online!

Pointfree 16 bytes

sum.enumFromTo 1

Try it online!

Direct, 17 bytes

f n=n*(n+1)`div`2

Try it online!

Standard fold, 19 bytes

f n=foldr(+)0[1..n]

Try it online!

share|improve this answer

Husk, 1 byte

Σ

Try it online!

Builtin! Σ in Husk is usually used to get the sum of all elements of a list, but when applied to a number it returns exactly n*(n+1)/2.

share|improve this answer
    
Out of curiosity, does this occur because the number is cast to a range and then summed, or is this actually hardcoded? – FryAmTheEggman 7 hours ago
1  
@FryAmTheEggman this is actually hardcoded, and is similar to the behavior of another builtin, Π, which can compute the product of all elements of a list or the factorial of a single number – Leo 7 hours ago
    
Σ is a two byte unicode character on my machine. I guess you use code page 1253? msdn.microsoft.com/en-us/library/cc195055.aspx – gmatht 3 hours ago
    
@gmatht Husk's code page – Jonathan Allan 2 hours ago

05AB1E, 2 bytes

LO

Try it online!

How it works

     #input enters stack implicitly
L    #pop a, push list [1 .. a]
 O   #sum of the list
     #implicit output 
share|improve this answer

JavaScript (ES6), 10 bytes

n=>n*++n/2

Example

let f =

n=>n*++n/2

console.log(f(5))

share|improve this answer

APL, 3 bytes

+/⍳

Try it online!

+/ - sum, - range.

share|improve this answer
    
This depends on the indexing. If indexing is set to 0, then you'd need an additional 2 bytes 1+ – Werner 8 hours ago
1  
@Werner indexing is default 1 so I didn't specify. its common here to specify only when using ⎕IO←0 (and it does not included in byte count) – Uriel 8 hours ago

Starry, 27 22 bytes

5 bytes saved thanks to @miles!

, + +  **       +   *.

Try it online!

Explanation

,             Read number (n) from STDIN and push it to the stack
 +            Duplicate top of the stack
 +            Duplicate top of the stack
  *           Pop two numbers and push their product (n*n)
*             Pop two numbers and push their sum (n+n*n)
       +      Push 2
   *          Pop two numbers and push their division ((n+n*n)/2)
.             Pop a number and print it to STDOUT
share|improve this answer
    
22 bytes. – miles 6 hours ago
    
@miles Thanks! Very good idea! – Luis Mendo 5 hours ago

Octave, 22 19 bytes

Because arithmetic operations are boring...

@(n)nnz(triu(e(n)))

Try it online!

Explanation

Given n, this creates an n×n matrix with all entries equal to the number e; makes all entries below the diagonal zero; and outputs the number of nonzero values.

share|improve this answer

Python 2, 24 16 bytes

-8 bytes thanks to FryAmTheEggman.

lambda n:n*-~n/2

Try it online!

share|improve this answer

Ohm, 2 bytes

Try it online!

Explanation

@Σ

    implicit input
@   inclusive range [1..input]
 Σ  sum
    implicit output
share|improve this answer

Retina, 13 bytes

.+
$*
1
$`1
1

Try it online! Explanation: The first and last stages are just unary ⇔ decimal conversion. The middle stage replaces each 1 with the number of 1s to its left plus another 1 for the 1 itself, thus counting from 1 to n, summing the values implicitly.

share|improve this answer

Mathematica, 9 bytes

#(#+1)/2&

Mathematica, 10 bytes

(#^2+#)/2&

Mathematica, 11 bytes

Tr@Range@#&

Mathematica, 12 bytes

i~Sum~{i,#}&

Mathematica, 15 bytes

Tr[#&~Array~#]&

Mathematica, 16 bytes

Binomial[#+1,2]&

Mathematica, 18 bytes

PolygonalNumber@#&

Mathematica, 19 bytes

#+#2&~Fold~Range@#&

Mathematica, 20 bytes

(by @Not a tree)

f@0=0;f@i_:=i+f[i-1]
share|improve this answer
    
It seems a shame to skip 13, 14 and 17… – Not a tree 4 hours ago
1  
It seems like a next challenge....or at least help me to complete the list. – Jenny_mathy 4 hours ago
    
You can have f@0=0;f@i_:=i+f[i-1] for 20 bytes, although that doesn't help fill the holes. – Not a tree 4 hours ago
    
I will add it because it differs! – Jenny_mathy 4 hours ago
    
17 bytes: ⌊(2#+1)^2/8⌋& (inspired by the OEIS page) – Not a tree 4 hours ago

Piet, 161 bytes / 16 codels

You can interpret it with this Piet interpreter or upload the image on this website and run it there. Not sure about the byte count, if I could encode it differently to reduce size.

Scaled up version of the source image:

rapapaing-image

Explanation

The highlighted text shows the current stack (growing from left to right), assuming the user input is 5:

1st transition Input a number and push it onto stack

5

2nd transition Duplicate this number on the stack

5 5

3rd transition Push 1 (the size of the dark red area) onto stack

5 5 1

4th transition Add the top two numbers

5 6

5th transition Multiply the top two numbers

30

6th transition The black area makes sure, that the cursor moves down right to the light green codel. That transition pushes 2 (the size of dark green) onto stack

30 2

7th transition Divide the second number on the stack by the first one

15

8th transition Pop and output the top number (interpreted as number)

[empty]

final trap By inserting a white area, the transition is a nop, the black traps our cursor. This ends execution of the program.

Original file (far too small for here): Original source image

share|improve this answer

8th, 21 12 bytes

Saved 9 bytes thanks to FryAmTheEggman

dup 1+ * 2 /

Usage and output

ok> : sum dup n:1+ * 2 / ;

ok> 5 sum .
15
share|improve this answer
2  
Actually, I think you saved 9 bytes thanks to Gauss ;) But thanks for the credit! – FryAmTheEggman 8 hours ago

BLua, 32 bytes

r=0;for i=1,n r=r+i end;return r

Try it out (Vanilla Lua version, 53 bytes)

I'm not very good at this

share|improve this answer

><>, 7+3 = 10 bytes

Calculates n(n+1)/2.
3 bytes added for the -v flag

:1+2,*n

Try it online!

Or if input can be taken as a character code:

><>, 9 bytes

i:1+2,*n;

Try it online!

share|improve this answer

Julia, 10 bytes

n->n*-~n/2

Try it online!

11 bytes (works also on Julia 0.4)

n->sum(1:n)

Try it online!

share|improve this answer

,,,, 6 bytes

:1+×2÷

Explanation

:1+×2÷

:       duplicate
 1+     add 1
   ×    multiply
    2÷  divide by 2

If I implement range any time soon...

share|improve this answer

cQuents, 2 bytes

;$

This is the type of question that cQuents was designed for, and the type of question I implemented the ; mode for. Take that, Oasis!

Try it online!

Explanation

;    Mode: Sum (output sum of sequence up to input)
 $   Each item in the sequence is its (1-based) index
share|improve this answer

PowerShell, 22 18 bytes

param($n)$n*++$n/2

Try it online!

Saved 4 bytes thanks to FryAmTheEggman. Uses Gauss' formula. Ho-hum.

share|improve this answer
    
@FryAmTheEggman You'd think so, and you'd be right. :p – AdmBorkBork 8 hours ago

C# (.NET Core), 10 bytes

n=>n++*n/2

Try it online!

share|improve this answer

CJam, 6 bytes

ri),:+

Try it online!

Explanation

ri    e# Read integer n
)     e# Add 1
,     e# Range from 0 to input argument minus 1
:+    e# Fold addition over array. Implicitly displa
share|improve this answer

Japt, 3 bytes

ò x

Explanation:

ò x
ò     Range [0...Input]
  x   Sum

Try it online!

share|improve this answer

Add++, 15 bytes

+?
V
+1
*G
/2
O

Try it online!

Just calculates the triangular numbers.

share|improve this answer

Braingolf, 3 bytes

U&+

Try it online!

U - range, &+ - sum.

share|improve this answer

R, 20 bytes

f=function(sum(1:n))
share|improve this answer
1  
Would be shorter if you used n=scan() instead of defining a function. – Max Lawnboy 3 hours ago
1  
And it would be even shorter to just use sum(1:scan()) – Giuseppe 2 hours ago

Swift, 13 bytes

Anonymous function:

{$0*($0+1)/2}

You can call it like this:

print({$0*($0+1)/2}(5))

Try it online!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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