# Sum of all integers from 1 to n

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 — Triangular numbers: a(n) = binomial(n+1,2) = n(n+1)/2 = 0 + 1 + 2 + ... + n.

Run the code snippet below to view a leaderboard for this question's answers. (Thanks to programmer5000 and steenbergh for suggesting this, and Martin Ender for creating it.)

• Closely related – FryAmTheEggman Jul 18 '17 at 20:36
• @FryAmTheEggman Sorry - had a bit of a brain fart there. I see what you mean. – GarethPW Jul 18 '17 at 20:45
• @Aaron you got ninja'd by Husk, which was just posted with a 1 byte solution – Skidsdev Jul 18 '17 at 21:35
• I suggest a stack snippet. – user58826 Jul 19 '17 at 11:42
• – Jerry Jeremiah Jul 27 '17 at 12:20

# J, 6 4 bytes

2!>:

Edit: I forgot that the binomial coefficient formula existed, so that lowers the bytecount. Also this is on the REPL or as a function with the input taken as the right argument. The other solutions need to be on the REPL, which I forgot to mention.

First post in a while, figured I'd submit the language I've been trying to learn recently. Not sure if you can specify one-indexing for ranges in J like with APL.

2!>:
>:  Increment
2!    n Choose 2

### Previous solution (6 bytes)

Explanation below

+/i.>:
i.    Range [0,n+1)
+/      Sum

### 7 byte solutions

Explanations beneath each

-:(*>:)
(*>:)  Hook: n * (n+1)
-:       Halve

-:(+*:)
(+*:)  Hook: n^2 + n
-:       Halve

{seq 1,_|sum}

Try it online!

# Vim, 4̶6̶ 251716 15 keystrokes

Thanks @CowsQuack for -9 bytes!

YP<C-a>Jr*0C<C-r>=<C-r>"/2⏎

Try it online!

### Ungolfed/Explained

YP                           " duplicate line containing N
<C-a>                      " increment the first line
J                     " join the two lines
r*                   " substitute space between N and N+1 with *
0C                 " delete line (store in " register) and insert
<C-r>=        ⏎  "   the expression
<C-r>"     "   from the " register
/2   "   divided by 2

# ArnoldC, 301 bytes

Well if Leo can find a way to do it in one byte, this is my way of throwing in the towel.

With the language based on the guy who never surrenders.

(And studying for the precalc final, you know, n(n+1)/2 is a formula I won't forget now, right?)

As of now, there's not really a way to take input in from the console from Try It Online, but this guy supposedly added something here.

Assuming that works, this code should do:

IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP 0
GET YOUR ASS TO MARS n
DO IT NOW
HEY CHRISTMAS TREE a
YOU SET US UP n
GET TO THE CHOPPER a
HERE IS MY INVITATION a
GET UP 1
YOU'RE FIRED n
ENOUGH TALK
TALK TO THE HAND a
YOU HAVE BEEN TERMINATED

If not, this should work, manually assigning variable n (although it's a bit against the challenge)

IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP 3
HEY CHRISTMAS TREE a
YOU SET US UP n
GET TO THE CHOPPER a
HERE IS MY INVITATION a
GET UP 1
YOU'RE FIRED n
ENOUGH TALK
TALK TO THE HAND a
YOU HAVE BEEN TERMINATED

Try it online!

# Pyt, 1 byte

Try it online!

Explanation:

implicit input
△             compute the nth triangle number
• Can you give a link to the codepage? – Adalynn Dec 24 '17 at 20:11
• It's at the beginning of the interpreter2 file - the language is still a work in progress – mudkip201 Dec 24 '17 at 20:34
• I just added a link to the codepage – mudkip201 Jan 12 '18 at 1:11
• 1 byte – caird coinheringaahing Jan 28 '18 at 21:04
• @cairdcoinheringaahing I never got around to editing all of my old answers once I made Pyt take input implicitly. I've edited some of them, but not all – mudkip201 Jan 28 '18 at 21:07

# brainfuck, 14 bytes

,[[>+<<.>-]>-]

Try it online!

Takes input as character code, outputs as unary null bytes

# shortC,  44  29 bytes

Bn){K"%d",&n);R"%d",n*(n+1)/2

Try it online!

Just a shortCed version of this. Any help would be appreciated.

;a;b;c;d@
REA a;
DIV b 2 c;
MUL a c d;
OUT d;

# Explanation

The code is an implementation of the n*(n+1)/2 solution seen in other responses. It should be fairly readable but here's the code annotated:

;a;b;c;d@    - Declare 4 vars a, b, c and d (@ marks end of declarations)
REA a;       - Read in the value from the console and store in a
ADD a 1 b;   - Add 1 to a and store in b
DIV b 2 c;   - Divide b by 2 and store in c
MUL a c d;   - Mulitply a and c and store in d
OUT d;       - Output d to console

n=>n*++n/2

Try it online!

# C (gcc), 19 bytes

#define f(n)n*-~n/2

Try it online!

# C (gcc), 15 bytes

f(n){n*=-~n/2;}

-5 bytes thanks to ceilingcat!

Try it online!

• @ceilingcat Huh. How does that work? – S.S. Anne Jan 10 '20 at 12:26
• @JL2210 It's abusing the way GCC compiles the code without optimizations. – NieDzejkob Jan 11 '20 at 14:37
• 1+2+3+4+5+6=18? – l4m2 Apr 21 at 10:53

# Hexagony, 16 bytes

?"&{2'<)}=*/@!:=
• You can get 15 pretty trivially by using chr(2). Try it online! – Jo King Jun 10 at 5:23
• @JoKing wow, I didn't even know that was a command, good find – Underslash Jun 10 at 5:31
• It's a sort of accidental extension to the "All Letters set the edge to their ordinal value" rule, in that any non-instruction non-whitespace character sets its ordinal value, even unicode characters! – Jo King Jun 10 at 5:38
• wait really? I feel a bit dumb now, and that opens up more possibilities – Underslash Jun 10 at 5:42

# Hexagony, 14 bytes

?'+\.*:){=/\!2

Expanded out, this is:

? ' +
\ . * :
) { = / \
! 2 . .
. . .

It took quite a bit of effort to get from a trivial 15 byte modification of Underslash's answer (using a char literal for 2), to get to 14 bytes. We save that one byte by managing to reuse all of '{= at the cost of an extra control flow instruction, as well as by terminating through an error of division by zero. There's still one no-op in the middle of this, so perhaps 13 is possible (but unlikely)?

Expanding it out by following the control flow, we find this:

?'+){=*'2{+*=2?*:!2)+*=2?*:

Removing all the extra stuff that are basically no-ops or are there to force a specific path of execution, we get:

?'+){=*'2{=:!=:
?                 Get the input
'+               Duplicate it to an adjacent edge
)              Increment it
{=*           Multiply the original by the incremented one
'2         Place 2 in an adjacent cell
{=:      Divide the result by 2
!     And print
=:   Divide by zero

# CJam, 6 bytes

ri),:+

Try it online!

### Explanation

,     e# Range from 0 to input argument minus 1
:+    e# Fold addition over array. Implicitly displa

# 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

# Swift, 13 bytes

Anonymous function:

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

You can call it like this:

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

Try it online!

#(#+1)/2&

or, at 11 bytes,

Tr@Range@#&

# AutoHotkey, 22 bytes

A(n){return n*(n+1)/2}

Defines a function that takes parameter n, and returns n*(n+1)/2, which is the nth triangle number, as shown in Leo's Husk answer.

• Are you sure about the - operator? – Olivier Grégoire Jul 18 '17 at 22:36
• @OlivierGrégoire Sure that it's the subtraction operator? I'm as sure as the AHK docs are – Skidsdev Jul 18 '17 at 22:38
• Okay, it's just seems weird given that 1+...+n is usually equals to n*(n+1)/2 in maths, not n*(n-1)/2, but I don't know AutoHotkey and how it does maths, so I had to ask. – Olivier Grégoire Jul 18 '17 at 22:44
• @OlivierGrégoire Oh right yeah no I'm a turd that's supposed to be a + – Skidsdev Jul 18 '17 at 22:53

# Tcl, 27 bytes

proc T n {expr $n*($n+1)/2}

Try it online!

# Casio Basic, 9 bytes

(n+1)n/2

8 bytes for the code, +1 to add n as parameter.

# Factor, 12 bytes

[ iota sum ]

Input is given as an argument to this anonymous function (quotation).

OP (1+:N)*(:N/2)

//
;-} {-
{-  -}<!-}!

Try it online!

~.)2/*

Try it online!

a*++a/2

Try it online!

# ><>, 20 15 bytes

:1-:?!v
n?=1l+<

@notatree saved me a couple bytes!

Try it online!

• Nice! You can save a byte by turning the second line into v?=1l+<, or if you don't mind exiting with an error, replace the second line with n?=1l+< and remove the last two lines. – Not a tree Jul 19 '17 at 7:42
• @Notatree Thanks! – steenbergh Jul 19 '17 at 7:55

# Carrot, 9 bytes

#^F+1/2*$Explanation: # //Set the string stack to the input ^ //Convert to operations mode F //Change to float stack +1 //Add one to the stack /2 //Divide the stack by 2 *$ //Multiply the stack by the input
//Implicitly output the result

&:1+*2/.@

Try it online!

# Gaia, 2 bytes

+⊢

This is reduce by addition +, which implicitly casts numbers to ranges beforehand.

You could also do ┅Σ (range and sum Σ), which is still 2 bytes.

# Common Lisp, 26 bytes

(lambda(n)(/(+(* n n)n)2))
• Hello, and welcome to the site! – user58826 Jul 19 '17 at 11:31

# Clojure, 3123 16 bytes

8 bytes saved thanks to @cliffroot

#(/(+(* % %)%)2)

Try it online!