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 code-golf, 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>
243 Answers 243
Bash script, 12 bytes
expr 1ドル + 2ドル
Save as add.sh, then run bash add.sh [argument] [argument].
-
1\$\begingroup\$ I think this should have been posted as an edit to your previous answer. \$\endgroup\$Dennis– Dennis2016年07月02日 22:10:10 +00:00Commented Jul 2, 2016 at 22:10
-
\$\begingroup\$ @Dennis These are somewhat separate: this one has to be run from a file, the other one can be run from the command line. \$\endgroup\$anna328p– anna328p2016年07月02日 22:12:57 +00:00Commented Jul 2, 2016 at 22:12
-
2\$\begingroup\$ Yes, I'm aware. I don't think they're different enough to warrant separate answers though. The other does exactly the same; it just wraps the program in a function declaration. \$\endgroup\$Dennis– Dennis2016年07月02日 22:18:09 +00:00Commented Jul 2, 2016 at 22:18
-
\$\begingroup\$ @Dennis Okay... \$\endgroup\$anna328p– anna328p2016年07月02日 22:18:57 +00:00Commented Jul 2, 2016 at 22:18
NotQuiteThere, 8 bytes
0-- 0- 0
Given that one of the aims of this languages was to be unable to perform addition, I think I failed.
How it works
# Implicit input; 10 and 20
0 # Push 0; STACK = [10 20 0]
- # Subtract; STACK = [10 -20]
- # Subtract; STACK = [-30]
0 # Push 0; STACK = [-30 0]
- # Subtract; STACK = [30]
0 # Push 0; STACK = [30 0]
# Output 30;
Whispers, 34 bytes
> Input
> Input
>> 1+2
>> Output 3
Don't worry, this doesn't just Output 3, or perform 1+2
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>
]
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>
]
Wumpus, 5 bytes
II+O@
Explanation
Straight-forward and boring:
I Read the first integer.
I Read the second integer.
+ Add them.
O Output the result.
@ Terminate the program.
Momema, 9 bytes
-8+*-8*-8
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())
Perl6, 3
Standalone function:
*+*
Complete program (19 bytes):
say [+] slurp.words
Pain-Flak, 6 bytes
)}{}{(
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.
FRACTRAN, 3 bytes
2/3
Take 2^a*3^b as input, return 2^(a+b).
I'm not sure whether or not extra bytes should be added for input encoding and output decoding.
6502 machine code - 8 bytes
A5 08 65 09 85 0A 4C 06
Input stored at 0x08 and 0x09, output storad at 0x0A
Re:direction, 9 characters, 3 bytes
♦►
♦►
◄ ▼
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).
RUST, 29 bytes
Save 5 bytes thank to ASCII-only
fn s(a:i32,b:i32)->i32{a+b}
ASCII-only also beat my solution here
-
1\$\begingroup\$ 8, also you have unneeded spaces anyway \$\endgroup\$ASCII-only– ASCII-only2019年01月29日 07:48:24 +00:00Commented Jan 29, 2019 at 7:48
-
\$\begingroup\$ @ASCII-only Thank you a lot for your solution. \$\endgroup\$Chau Giang– Chau Giang2019年01月29日 08:00:41 +00:00Commented Jan 29, 2019 at 8:00
-
\$\begingroup\$ You forgot to rename the function \$\endgroup\$ASCII-only– ASCII-only2019年01月29日 08:02:33 +00:00Commented Jan 29, 2019 at 8:02
-
1\$\begingroup\$ lol, now it's 27 bytes not 29 \$\endgroup\$ASCII-only– ASCII-only2019年01月29日 08:10:50 +00:00Commented 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– Chau Giang2019年01月29日 08:12:06 +00:00Commented Jan 29, 2019 at 8:12
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.
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
-
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– Value Ink2019年09月21日 03:03:15 +00:00Commented Sep 21, 2019 at 3:03
Seed, 11 bytes
4 141745954
Damn, It's quite small
-
\$\begingroup\$ This appears to correspond with the program
&&+., which loops infinitely outputting zeroes. You need an@afterwards \$\endgroup\$Jo King– Jo King2020年01月07日 23:45:16 +00:00Commented Jan 7, 2020 at 23:45
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
.
Pepe 19 bytes
REeerEeeREEEEEEReEE
Try it not online but at the online interpreter
My first Pepe answer... made me want to reeeeeee (not reeally)
REee #Input first integer
rEee #Input second integer
REEEEEE #Add two integers
ReEE #Print result as integer
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
Python 3, 14 bytes
lambda x,y:x+y
Pretty self-explanatory lol
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.
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
}
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 ).
-
\$\begingroup\$ Non-competing status hasn't been in use for almost 4 years. Your answer is competing. \$\endgroup\$Makonede– Makonede2021年04月16日 17:14:40 +00:00Commented Apr 16, 2021 at 17:14
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.
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.
abcn, 3 bytes
@@x
Explanation :
@ ---> take input 1
@ ---> input 2
---> nothing in between so they auto get added
x ---> print
-
\$\begingroup\$ The builtin
aby itself is a valid solution to this challenge. \$\endgroup\$DLosc– DLosc2022年02月25日 14:54:39 +00:00Commented Feb 25, 2022 at 14:54
PostScript, 3 bytes
add
This consumes two arguments from top of stack and pushes its result to the stack.
5 16is inputted as005 016\$\endgroup\$