85
\$\begingroup\$

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.

Leaderboard

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

var QUESTION_ID=133109,OVERRIDE_USER=69148;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px} /* font fix */ body {font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;} /* #language-list x-pos fix */ #answer-list {margin-right: 200px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
10
  • 5
    \$\begingroup\$ Closely related \$\endgroup\$ Jul 18, 2017 at 20:36
  • \$\begingroup\$ @FryAmTheEggman Sorry - had a bit of a brain fart there. I see what you mean. \$\endgroup\$
    – GarethPW
    Jul 18, 2017 at 20:45
  • 2
    \$\begingroup\$ @Aaron you got ninja'd by Husk, which was just posted with a 1 byte solution \$\endgroup\$
    – Mayube
    Jul 18, 2017 at 21:35
  • 7
    \$\begingroup\$ I suggest a stack snippet. \$\endgroup\$
    – user58826
    Jul 19, 2017 at 11:42
  • 1
    \$\begingroup\$ Related: minecraftforum.net/forums/off-topic/… \$\endgroup\$ Jul 27, 2017 at 12:20

222 Answers 222

1 2
3
4 5
8
2
\$\begingroup\$

Neim, 3 2 bytes

First Neim answer

𝐈𝐬

Try it online!

Explanation

𝐈   # Gets inclusive range from 0 to input
𝐬   # Sum the list

Saved a byte due to Okx

\$\endgroup\$
2
  • \$\begingroup\$ Nein has implicit input, so you can remove the first byte :) \$\endgroup\$
    – Okx
    Jul 19, 2017 at 8:47
  • \$\begingroup\$ @Okx I could've sworn i tried that, thanks. \$\endgroup\$
    – LiefdeWen
    Jul 19, 2017 at 8:56
2
\$\begingroup\$

C, 56 47 bytes

main(n){scanf("%d",&n);printf("%d",n*(n+1)/2);}

This is my first attempt at any code golf of any kind. Submitted as I saw that there were no other answers for C.

Old code:

int main(){int n;scanf("%d",&n);printf("%d",n*(n+1)/2);}

Thanks to programmer5000 for the help. :)

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome to the site! Nice first golf! You may be interested in some tips for golfing in C. \$\endgroup\$
    – user58826
    Jul 19, 2017 at 11:24
  • \$\begingroup\$ you can use -~n instead of (n+1) to make it 45 bytes main(n){scanf("%d",&n);printf("%d",n*-~n/2);} :) \$\endgroup\$ Feb 12, 2022 at 5:43
2
\$\begingroup\$

MUMPS, 15 bytes

r n w !,n*n+n/2

Accepts user input (r n) and writes a new line along with the sum (w !,n*n+n/2). Order of operations doesn't matter in MUMPS: It goes from left to right except when there are parentheses.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Stephen
    Jul 19, 2017 at 13:04
2
\$\begingroup\$

Excel, 12 bytes

=(A1+1)/2*A1

Or, alternatively:

=(A1^2+A1)/2

Instead of counting all n elements, take the average of the n elements, and multiply it by the number of elements.

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

R, 13 bytes

sum(1:scan())

Saved 9 bytes thanks to Giuseppe and Max Lawnboy.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Would be shorter if you used n=scan() instead of defining a function. \$\endgroup\$ Jul 19, 2017 at 2:22
  • 4
    \$\begingroup\$ And it would be even shorter to just use sum(1:scan()) \$\endgroup\$
    – Giuseppe
    Jul 19, 2017 at 2:47
  • \$\begingroup\$ This is not a valid R code. I believe this is what you were aiming at f=function(n)sum(1:n). \$\endgroup\$
    – djhurio
    Jul 19, 2017 at 9:50
  • 1
    \$\begingroup\$ ^ You're right, I think when I removed the spaces I was too quick as I did run it in R and just c/p'ed it over. \$\endgroup\$ Jul 19, 2017 at 10:42
  • \$\begingroup\$ It should be sum(1:scan()), not sum(1:scan(n)). \$\endgroup\$
    – djhurio
    Jul 19, 2017 at 20:59
2
\$\begingroup\$

Forth, 17 bytes

Defines a word (function) that returns n*(n+1)/2.

: f dup 1+ * 2/ ;

Try it online


Full program with the same byte count:

key dup 1+ * 2/ .

Try it online - input is a single character, like BF.

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

C, 22 bytes

With preprocessor

#define F(n) (n+1)*n/2

or 24 bytes with code and math

F(n){return (n+1)*n/2;}

or 27 bytes with recursive code

F(n){return n?n+F(n-1):0;}
\$\endgroup\$
4
  • 4
    \$\begingroup\$ eliminate spaces: #define F(N)(n+1)*n/2, F(N){return(n+1)*n/2}; \$\endgroup\$
    – Uriel
    Jul 18, 2017 at 23:13
  • 1
    \$\begingroup\$ And for the "code and math" section, you don't need the semicolon after the }. \$\endgroup\$
    – Adalynn
    Jul 19, 2017 at 17:23
  • \$\begingroup\$ Um ... actually the first two solutions don't work... C is case sensitive! \$\endgroup\$
    – Adalynn
    Jul 23, 2017 at 16:18
  • \$\begingroup\$ also change (n+1) to -~n: #define F(n)-~n*n/2 \$\endgroup\$ Oct 17, 2019 at 0:17
2
\$\begingroup\$

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.

Explanation

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

Previous solution (6 bytes)

Explanation below

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

7 byte solutions

Explanations beneath each

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

-:(+*:)
  (+*:)  Hook: n^2 + n
-:       Halve
\$\endgroup\$
2
\$\begingroup\$

Röda, 13 bytes

{seq 1,_|sum}

Try it online!

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

Vim, 4̶6̶ 25 17 16 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

inside vim

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

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
WHO IS YOUR DADDY AND WHAT DOES HE DO
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
HE HAD TO SPLIT 2
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
HE HAD TO SPLIT 2
ENOUGH TALK
TALK TO THE HAND a
YOU HAVE BEEN TERMINATED

Try it online!

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

Pyt, 1 byte

Try it online!

Explanation:

               implicit input
 △             compute the nth triangle number
\$\endgroup\$
5
  • \$\begingroup\$ Can you give a link to the codepage? \$\endgroup\$
    – Adalynn
    Dec 24, 2017 at 20:11
  • \$\begingroup\$ It's at the beginning of the interpreter2 file - the language is still a work in progress \$\endgroup\$
    – mudkip201
    Dec 24, 2017 at 20:34
  • \$\begingroup\$ I just added a link to the codepage \$\endgroup\$
    – mudkip201
    Jan 12, 2018 at 1:11
  • \$\begingroup\$ 1 byte \$\endgroup\$ Jan 28, 2018 at 21:04
  • \$\begingroup\$ @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 \$\endgroup\$
    – mudkip201
    Jan 28, 2018 at 21:07
2
\$\begingroup\$

brainfuck, 14 bytes

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

Try it online!

Takes input as character code, outputs as unary null bytes

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

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.

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

Spice (56 bytes)

;a;b;c;d@
REA a;
ADD a 1 b;
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
\$\endgroup\$
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 29 22 10 bytes

n=>n*++n/2

Try it online!

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

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!

\$\endgroup\$
4
  • \$\begingroup\$ @ceilingcat Huh. How does that work? \$\endgroup\$
    – S.S. Anne
    Jan 10, 2020 at 12:26
  • 1
    \$\begingroup\$ @JL2210 It's abusing the way GCC compiles the code without optimizations. \$\endgroup\$
    – Maya
    Jan 11, 2020 at 14:37
  • 1
    \$\begingroup\$ 1+2+3+4+5+6=18? \$\endgroup\$
    – l4m2
    Apr 21, 2021 at 10:53
  • \$\begingroup\$ as stated by @l4m2, the second solution doesn't work for even values of n, this can be fixed by implicitly casting to double for +1 byte: f(n){n*=-~n/2.;} \$\endgroup\$
    – c--
    Jan 31, 2023 at 18:15
2
\$\begingroup\$

Hexagony, 16 bytes

?"&{2'<)}=*/@!:=

Try it online! Try it online differently!

\$\endgroup\$
4
  • \$\begingroup\$ You can get 15 pretty trivially by using chr(2). Try it online! \$\endgroup\$
    – Jo King
    Jun 10, 2021 at 5:23
  • \$\begingroup\$ @JoKing wow, I didn't even know that was a command, good find \$\endgroup\$
    – Underslash
    Jun 10, 2021 at 5:31
  • \$\begingroup\$ 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! \$\endgroup\$
    – Jo King
    Jun 10, 2021 at 5:38
  • \$\begingroup\$ wait really? I feel a bit dumb now, and that opens up more possibilities \$\endgroup\$
    – Underslash
    Jun 10, 2021 at 5:42
2
\$\begingroup\$

Fourier, 9 bytes

I~_^*_/2o

Try it online!

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

Knight, 11 bytes

O/*+1=xPx 2

Try it online!

Ungolfed:

OUTPUT / (* (+1 = x PROMPT) x) 2

How the magic works:

This is using the fact that \$\sum_{n=1}^{x} n == \frac{x(x + 1)}{2}\$

  1. =xP First, we read a line from standard input and assign to x. Normally, we would do +0P to immediately coerce it to a Number, but I have better plans.
  2. +1=xP We add the result of the assignment to \$1\$. This coerces to Number for us, giving a result of Number.
  3. *+1=xPx We then multiply that by x. Since +1=xP is a Number, x will also be converted to a Number. Now, we have \$x(x + 1)\$.
  4. Last, divide by 2 and output.
\$\endgroup\$
2
\$\begingroup\$

MSWLogo, 26 bytes

to f :n
pr :n*(:n+1)/2
end

Proof of Correctness

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

Vyxal Rs, 0 bytes


Try it Online!

Lol

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

Regex 🐇 (Perl / Raku / PCRE), 2 bytes

x+

Takes its input in unary, as a string of x characters whose length represents the number. Returns its output as the number of ways the regex can match.

Try it online! - Perl v5.28.2 / Attempt This Online! - Perl v5.36+
Try it online! - Raku
Try it online! - PCRE1
Try it online! - PCRE2 v10.33 / Attempt This Online! - PCRE2 v10.40+

Although Perl, Raku, and PCRE2 are the only regex engines currently capable of counting the number of possible matches without their source code being patched, this solution itself obviously only uses the most basic regex functionality, universal to all regex engines.

x+ will match every possible non-empty substring exactly once. So for example with the input 5, it is converted to unary as xxxxx, and then the list of possible matches is the following set of substrings:

xxxxx, xxxx, xxx, xx, x
xxxx, xxx, xx, x
xxx, xx, x
xx, x
x

As such, counting these is equivalent to calculating the sum of all integers from \$1\$ to \$n\$ (a.k.a. the \$n\$th triangular number).

This can be extended in many interesting ways, for example raising numbers to a constant power.

Retina 1.0, 9 bytes

.+
*
w`.+

Input and output are both in decimal.

Try it online!

Explanation:

.+
*

Convert the input from decimal to unary, with _ as the repeating character.

w`.+

w enables full overlap mode, where all substrings are considered (this feature was introduced in Retina 1.0.0). .+ then matches the full set of non-empty substrings. Using .* instead, to match all substrings, would shift the sequence 1 forward: Try it online!

If the input were taken in unary, this solution would be 4 bytes: Try it online!

Retina is not capable of counting the number of ways a regex can match, only the number of unique substrings that match, so this method can't be directly extended any further than the triangular number sequence.

\$\endgroup\$
9
  • \$\begingroup\$ Are there any other interesting sequences not exceeding n(n+1)/2 that could be calculated by Retina 1 in this way? \$\endgroup\$
    – Neil
    Jul 24, 2022 at 8:42
  • 1
    \$\begingroup\$ Presumably constant exponentials would be just a case of e.g ^(.|.|.)*$ for 3ⁿ. How about Fibonacci numbers, does something like ^(.|..)*$ work? \$\endgroup\$
    – Neil
    Jul 24, 2022 at 8:44
  • \$\begingroup\$ @Neil The maximum for Retina 1.0's w is (n+1)(n+2)/2 which is reached by .* (there are n+1 empty substrings). Not sure if there are any other interesting sequences this would work for. \$\endgroup\$
    – Deadcode
    Jul 24, 2022 at 9:05
  • 1
    \$\begingroup\$ @Neil Fibonacci is what kicked off this idea for me :-) and I even originally used ^(.|..)*$ for it instead of ^(..?)*$. \$\endgroup\$
    – Deadcode
    Jul 24, 2022 at 9:05
  • \$\begingroup\$ @Neil Nice way of doing \$k^n\$. I hadn't even started thinking about that yet. I guess ^(.(||))*$ would golf it better for \$k>3\$. \$\endgroup\$
    – Deadcode
    Jul 24, 2022 at 9:14
2
\$\begingroup\$

BitCycle -u, 11 10 bytes

-1 byte thanks to Jo King

 ~\~!
?AB^

Try it here!

Explanation

In BitCycle, numbers are represented in unary (with the -u flag allowing I/O to be given in decimal form for convenience). Thus, all we need is this algorithm:

  • Input a number
  • Loop while the number is nonzero:
    • Output the current number
    • Decrement the current number

Outputting unary numbers consecutively has the effect of adding them, thus giving n + (n-1) + ... + 1.

The number (as a string of 1 bits) comes in at the source ? and goes into the collector A, which then dumps it into the collector B. When the bits exit B, they are directed north into the dupneg ~, where a copy turns right (east) and a negated copy turns left (west). The original copy goes into the sink ! to be output. From the negated copy, one bit is sent north by the splitter \ and discarded. The rest get negated again by a second dupneg; the doubly negated bits go south into the A collector to begin the loop again, while the other copies go north off the playfield and are discarded. Once no bits are left, the program halts and outputs the sink's contents.

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

flax, 2 bytes

Σκ

Try It Online!

Explanation

Σκ
Σ     ⍝ sum of
 κ    ⍝ numbers from range 1...n
\$\endgroup\$
2
\$\begingroup\$

awk, 18 bytes

jot 50 | 
awk '$_*=$++_++/(_+_--)'
1
3
6
10
15
21
28
36
45
55
66
78
91
105
\$\endgroup\$
2
  • \$\begingroup\$ What does jot do here? \$\endgroup\$
    – pxeger
    Dec 19, 2022 at 12:53
  • \$\begingroup\$ @pxeger : visually representing a sequentially incrementing integer counter …. the output was 50 rows long, each representing the sum up to that row #, but i trimmed it to 105 for readability. it's basically like seq but i like it more cuz it requires fewer invocations of the -f fmt flag \$\endgroup\$ Dec 20, 2022 at 2:09
2
\$\begingroup\$

Japt -mx, 1 byte

Ä

Try it

Ä     :Implicit map of each U in the range [0,input)
Ä     :Add 1
      :Implicit output of sum of resulting array
\$\endgroup\$
2
\$\begingroup\$

Nibbles, 1 byte (2 nibbles)

+,

No builtin for sum-over-list or flags for summation or range, but Nibbles still seems pretty competitive here...

+       # sum of list:
 ,      # range from 1..
        # (implicit) input
\$\endgroup\$
2
\$\begingroup\$

shell, 17 14 bytes

First attempt was:

seq -s+ -t0 $1|bc

But not all implementations of seq know -t option. Second attempt is:

seq -s+0 $1|bc
\$\endgroup\$
4
  • \$\begingroup\$ there is no -t option to seq(1), but seq -s+ 0 $1|bc or seq -s+ $1|bc works \$\endgroup\$
    – c--
    Jan 31, 2023 at 18:06
  • \$\begingroup\$ mine knows -t but it seems that GNU/Linux and OpenBSD implementations doesn't. I suppose your implementation still terminates with new-line, cause here seq -s+ 5 yields 1+2+3+4+5+ which turns to "Parse error: bad expression" for bc \$\endgroup\$
    – gildux
    Jan 31, 2023 at 19:44
  • \$\begingroup\$ 13 bytes bash solution from 2017.. \$\endgroup\$
    – roblogic
    Mar 23, 2023 at 17:00
  • \$\begingroup\$ I've missed that post @roblogic :( It has the same issue (my MacOS and my FreeBSD are resulting to the parse error) \$\endgroup\$
    – gildux
    Mar 24, 2023 at 18:15
2
\$\begingroup\$

Erlang 19 bytes

s(N) -> N*(N+1)/2.
\$\endgroup\$
1 2
3
4 5
8

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.