68
\$\begingroup\$

Input: Two integers. Preferably decimal integers, but other forms of numbers can be used. These can be given to the code in standard input, as arguments to the program or function, or as a list.

Output: Their sum. Use the same format for output integers as input integers. For example, the input 5 16 would lead to the output 21.

Restrictions: No standard loopholes please. This is , answer in lowest amount of bytes wins.

Notes: This should be fairly trivial, however I'm interested to see how it can be implemented. The answer can be a complete program or a function, but please identify which one it is.

Test cases:

1 2 -> 3
14 15 -> 29
7 9 -> 16
-1 8 -> 7
8 -9 -> -1
-8 -9 -> -17

Or as CSV:

a,b,c
1,2,3
14,15,29
7,9,16
-1,8,7
8,-9,-1
-8,-9,-17

Leaderboard

var QUESTION_ID=84260,OVERRIDE_USER=8478;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}
<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\$
3
  • 31
    \$\begingroup\$ This is quite trivial, but not really simpler than, e.g., the Hello World catalog. Given that the ability to add integers is one of our two requirements for programming languages, I'd say it's worthwhile to have if properly specified. \$\endgroup\$
    – Dennis
    Commented Jul 2, 2016 at 0:48
  • 2
    \$\begingroup\$ Can the answer take input with preceding zeros as default? e.g. 5 16 is inputted as 005 016 \$\endgroup\$
    – FinW
    Commented Dec 4, 2016 at 11:56
  • 1
    \$\begingroup\$ @FinW Sure. As long as they don't get interpreted as octal. \$\endgroup\$
    – anna328p
    Commented Dec 4, 2016 at 20:47

237 Answers 237

1
4 5
6
7 8
1
\$\begingroup\$

Perl 5, 7 bytes

6 bytes code + 1 for -p.

$_+=<>

Try it online! (-l added for readability.)

\$\endgroup\$
1
\$\begingroup\$

Whispers, 34 bytes

> Input
> Input
>> 1+2
>> Output 3

Try it online!

Don't worry, this doesn't just Output 3, or perform 1+2

\$\endgroup\$
1
\$\begingroup\$

Verbosity, 388 bytes

Include<Input>
Include<Output>
Include<Integer>
Include<MetaFunctions>
Input:DefineVariable<i; 0>
Output:DefineVariable<o; 0>
Integer:DefineVariable<f; Input:ReadEvaluatedLineFromInput<i>>
Integer:DefineVariable<s; Input:ReadEvaluatedLineFromInput<i>>
Integer:DefineVariable<r; Integer:Sum<f; s>>
Output:DisplayAsText<o; r>
DefineMain<> [
MetaFunctions:ExecuteScript<MetaFunctions@FILE>
]

Try it online!

Outputs as Integer<result> (the natural representation of integers in Verbosity)

Ungolfed

Include<Input>
Include<Output>
Include<Integer>
Include<MetaFunctions>

Input:DefineVariable<in; 0>
Output:DefineVariable<out; 0>

Integer:DefineVariable<first; Input:ReadEvaluatedLineFromInput<in>>
Integer:DefineVariable<second; Input:ReadEvaluatedLineFromInput<in>>

Integer:DefineVariable<result; Integer:Sum<first; second>>

Output:DisplayAsText<out; result>

DefineMain<> [
	MetaFunctions:ExecuteScript<MetaFunctions@FILE>
]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Wumpus, 5 bytes

II+O@

Try it online!

Explanation

Straight-forward and boring:

I   Read the first integer.
I   Read the second integer.
+   Add them.
O   Output the result.
@   Terminate the program.
\$\endgroup\$
1
\$\begingroup\$

Forked, 5 bytes

$$+%&

Try it online!

  • $$ - read two integers
  • + - add top two stack values
  • % - print top of stack as integer
  • & - "terminate", prevents the IP from wrapping
\$\endgroup\$
1
\$\begingroup\$

Momema, 9 bytes

-8+*-8*-8

Try it online!

Momema uses prefix syntax, and assignment statements are implicit (simply writing two expressions ab acts as *a = b;), so this program looks like this:

*(-8) = *(-8) + *(-8)

The cell -8 in Momema is memory-mapped for numeric I/O. Reading from it causes input, and writing to it causes output.

print_num(read_num() + read_num())
\$\endgroup\$
1
\$\begingroup\$

Perl6, 3

Standalone function:

*+*

Complete program (19 bytes):

say [+] slurp.words

\$\endgroup\$
1
+50
\$\begingroup\$

Pain-Flak, 6 bytes

)}{}{(

Try it online!

Pain-Flak is Brain-Flaks evil twin. When translated into regular brain-flak, we get:

({}{})({}{})

The first one is the standard addition snippet. The second one is effectively a NO-OP.

\$\endgroup\$
1
\$\begingroup\$

FRACTRAN, 3 bytes

2/3

Take 2^a*3^b as input, return 2^(a+b).

Try it online!

I'm not sure whether or not extra bytes should be added for input encoding and output decoding.

\$\endgroup\$
1
\$\begingroup\$

6502 machine code - 8 bytes

A5 08 65 09 85 0A 4C 06

Input stored at 0x08 and 0x09, output storad at 0x0A

\$\endgroup\$
1
\$\begingroup\$

Re:direction, 9 characters, 3 bytes

♦►
♦►
◄ ▼

Try it online!

In Re:direction's packed encoding:

F1 F1 B6

(Byte F1 encodes ♦►␤; B6 encodes ◄ ▼).

Explanation

Re:direction is a 2D language that uses a queue of direction commands. At the start of the program, the queue is initialized from user input; an integer n becomes n copies of , then . From that point onwards, whenever we use a direction command, it controls the direction in which the instruction pointer moves but also gets pushed onto the tail of the queue.

The command shifts one direction from that queue, and moves in that direction. So ♦► is effectively a loop that moves any number of from the head of the queue to the tail; as long as the queue starts with , we'll shift it and go right, and hit the in the program, which sends the IP right back where it started (wrapping around the edge of the program) and pushes the to the tail of the queue instead. Once we hit the , it gets deleted from the queue and we move onwards.

We can note that ♦► does not preserve the in the queue. As such, running it twice deletes both from the initial queue, leaving us with a number of equal to the sum of the initial two numbers.

We need to encode the output the same way as the input, meaning that we need to add a and halt the program. A direction command that points to itself will run and then immediately halt the program, so we can use a on a column by itself to do both jobs. We do, however, need to get there; this program uses a to do so (because stray ups and lefts in the queue won't affect the output).

\$\endgroup\$
1
\$\begingroup\$

RUST, 29 bytes

Save 5 bytes thank to ASCII-only

Try this on line

fn s(a:i32,b:i32)->i32{a+b}

ASCII-only also beat my solution here

\$\endgroup\$
7
  • 1
    \$\begingroup\$ 8, also you have unneeded spaces anyway \$\endgroup\$
    – ASCII-only
    Commented Jan 29, 2019 at 7:48
  • \$\begingroup\$ @ASCII-only Thank you a lot for your solution. \$\endgroup\$
    – Chau Giang
    Commented Jan 29, 2019 at 8:00
  • \$\begingroup\$ You forgot to rename the function \$\endgroup\$
    – ASCII-only
    Commented Jan 29, 2019 at 8:02
  • 1
    \$\begingroup\$ lol, now it's 27 bytes not 29 \$\endgroup\$
    – ASCII-only
    Commented Jan 29, 2019 at 8:10
  • \$\begingroup\$ @ASCII-only, thank you, I just updated it, I count all bytes with my eyes so maybe I was wrong \$\endgroup\$
    – Chau Giang
    Commented Jan 29, 2019 at 8:12
1
\$\begingroup\$

Obx, 3 bytes

Obx is an abandoned language created by Phase in 2016.

+xy

With an input of 1 and 2, this program would output 3. Let's learn why.

+xy creates a function that adds x (the first argument) and y (the second argument). The last function created is called with whatever the input is.

\$\endgroup\$
1
\$\begingroup\$

Owk, 13 bytes

Owk is an abandoned language created by Phase.

a:λx.λy.x+y

This is written in an unofficial fork(but it's shorter):

a:x+y
\$\endgroup\$
1
  • 2
    \$\begingroup\$ What do you mean Owk doesn't support input? Don't you have lambdas? A lambda that takes variables as arguments and returns a value counts as valid input/output. Hardcoding two characters into your program is not. \$\endgroup\$
    – Value Ink
    Commented Sep 21, 2019 at 3:03
1
\$\begingroup\$

Seed, 11 bytes

4 141745954

Damn, It's quite small

\$\endgroup\$
1
  • \$\begingroup\$ This appears to correspond with the program &&+., which loops infinitely outputting zeroes. You need an @ afterwards \$\endgroup\$
    – Jo King
    Commented Jan 7, 2020 at 23:45
1
\$\begingroup\$

Re:direction, 9 bytes

▲♦▼
►♦
 ▲

Suprisingly small for a language like this. Re:direction is a language that only uses arrows, a queue, and to go in the direction of the first item on the queue

Explanation

Note: I'll be using .'s to represent instructions I'm not talking about, and I'll split it up into a couple of sections

Input is automatically pushed to the queue as ►'s seperated by ▼'s

▲..
►.      Pushes ▲ into the queue, used as a marker later
 .

...
►♦       Loop that goes through every item of the queue (the input). If its ►, it does nothing to it.
 ▲       If it is ▼, it gets replaced by ▲, which is ignored by the output

.♦▼      If it is an ▲, another ♦ is run, which goes right and removes one item from the queue to make the output correct
.♦       Then, since the ▼ is the only arrow in it's column, it gets pushed to the queue then the program halts
 .

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Cascade, 4 bytes

#&
+

Pretty self explanatory, uses wrapping so there is only 1 input instruction

Try it online!

\$\endgroup\$
1
\$\begingroup\$

hashmap, 10 bytes

i" "ĥdĐ+

Explanation:

i" "ĥdĐ+
i        Take input
 " "     Push space
    ĥ    Split the input by space
     dĐ  Convert the list to a double then flatten the list
       + Add them together
\$\endgroup\$
1
\$\begingroup\$

Python 3, 14 bytes

lambda x,y:x+y

Pretty self-explanatory lol

\$\endgroup\$
1
\$\begingroup\$

Mornington Crescent, 403 bytes

This challenge is pretty trivial given Mornington Crescent's many functions, but it still clocks in at 403 bytes due to the syntax of the language itself.

Take Northern Line to Bank
Take Circle Line to Bank
Take District Line to Parsons Green
Take District Line to Bank
Take Circle Line to Hammersmith
Take District Line to Parsons Green
Take District Line to Parsons Green
Take District Line to Upminster
Take District Line to Bank
Take District Line to Upminster
Take District Line to Bank
Take Circle Line to Bank
Take Northern Line to Mornington Crescent

The program parses the two numbers from the input string via Parsons Green, adds them in Upminster, and returns the result. This is slightly complicated by the fact that a number must be used to 'reset' Parsons Green after it has been used once so it doesn't overwrite any data.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Swift, 12 bytes

Without types

{a,b in a+b}
{ a, b in // take two parameters
  a + b  // return sum
}

With types (21 bytes)

{(a:Int,b:Int)in a+b}
{ (a: Int, b: Int) in // take two `Int` parameters
  a + b               // return sum
}
\$\endgroup\$
1
\$\begingroup\$

Branch, 2 bytes

+#

Try it on the online Branch interpreter!

Explanation

+ adds the values of the children of the current node. If any nodes are missing, they are usually created with a value of 0; however, binary operations will instead read from STDIN (this usually gets 0 if the input is exhausted, but if the input was empty to begin with, feof doesn't seem to work the way I want it to).

# just outptus the value of the current node as a number (as opposed to a character, which . does)

\$\endgroup\$
1
\$\begingroup\$

Concurr, pre-pre-release, 2 bytes

$+

A number of items within parentheses is a call, and calling a function with zero arguments returns that function because of currying. This could not just be + because Concurr is a lisp-2, meaning it has a separate namespace for global functions and local variables. $ expr is a shortcut for ( expr ).

\$\endgroup\$
1
  • \$\begingroup\$ Non-competing status hasn't been in use for almost 4 years. Your answer is competing. \$\endgroup\$
    – Makonede
    Commented Apr 16, 2021 at 17:14
1
\$\begingroup\$

A0A0, 28 bytes

I0A1V0O0
I0V0S0
G-2G-1G-1G-1

Each number is first put into two separate operands (V0 instructions) on two lines via the I0 instructions on each line and then later combined together for adding. After taking input, we jump to the top. The A1 adds the V0 O0 onto the next line, to get the following code: V2 S0 V1 O0. I've labelled the operands with V1 and V2 to represent the different operands. During execution these contain the actual inputs. From there it's simple.

V2 S0 V1 O0
V2          ; operand, put into the S0 next to it
   S0       ; adds its value to the operand next to it (V1)
      V1    ; operand, put into the O0 next to it
         O0 ; outputs its value as a number

The line of jumps at the bottom just goes back to the correct line each time.

\$\endgroup\$
1
\$\begingroup\$

CSASM v2.5.1, 21 bytes

func a:
add
print
ret
end

Defines a function named a which pops two number-type values of the same type from the stack and prints their sum.

\$\endgroup\$
1
\$\begingroup\$

abcn, 3 bytes

@@x

Explanation :

@                ---> take input 1
@                ---> input 2
                 ---> nothing in between so they auto get added
x                ---> print
\$\endgroup\$
1
\$\begingroup\$

Regenerate, 10 bytes

${$~1+$~2}

Takes the input numbers as command-line arguments. Try it here!

Explanation

${       }  Treat the result of this expression as a string:
  $~1        First cmdline arg
     +       Plus
      $~2    Second cmdline arg
\$\endgroup\$
1
\$\begingroup\$

tinylisp, 19 bytes

(d f(q((x y)(a x y 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ The builtin a by itself is a valid solution to this challenge. \$\endgroup\$
    – DLosc
    Commented Feb 25, 2022 at 14:54
1
\$\begingroup\$

PostScript, 3 bytes

add

This consumes two arguments from top of stack and pushes its result to the stack.

\$\endgroup\$
1
\$\begingroup\$

Ark

<<+^

example: [1, 4, 5]

\$\endgroup\$
1
4 5
6
7 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.