Add N numbers without using + or -

Given N integers, output the sum of those integers.

Input

You may take the integers in any reasonable format, including:

• stdin
• arguments
• an array or list

Testcases

n=2, 1, 2 -> 3
n=2, 2, 2 -> 4
n=2, 9, 10 -> 19
n=2, 7, 7 -> 14
n=2, 8, 8 -> 16
n=2, -5, 3 -> -2
n=2, -64, -64 -> -128
n=2, -3, 0 -> -3
n=2, 0, 3 -> 3
n=2, 0, 0 -> 0
n=2, -1, -1 -> -2
n=2, -315, -83 -> -398
n=2, 439, 927 -> 1366
n=3, 1, 2, 3 -> 6
n=3, 2, 2, 5 -> 9
n=3, 0, 9, 10 -> 19
n=3, 7, 0, 7 -> 14
n=3, 8, 8, 0 -> 16
n=3, -5, 3, -2 -> -4
n=3, -64, -64, 16 -> -112
n=3, -3, 0, 0 -> -3
n=3, 0, 3, 0 -> 3
n=3, 0, 0, 0 -> 0
n=3, -1, -1, -1 -> -3
n=3, -315, -83, -34 -> -432
n=3, 439, 927, 143 -> 1509
n=17, -74, 78, 41, 43, -20, -72, 89, -78, -12, -5, 34, -41, 91, -43, -23, 7, -44 -> -29


Testcases with partial sums that exceed 8 bits are only required if your integer type supports each partial sum (if any) and the final result. As a special case, n=2, -64, -64 -> -128 is only required if your integer type can represent -128.

Testcases involving negative integers are only required if your language natively supports negative integers.

• I have one which I think is neat enough to be a notable mention though it doesn't strictly abide by the rules. Instead of summing a list, it only adds 2 numbers... Any chance I can still add it and let the votes decide? Commented Mar 24, 2020 at 2:26
• @AviF.S., that would not be a valid solution to the challenge. Commented Mar 24, 2020 at 13:37
• @Avi If you create another function that does the rest of the adding, then yes. Otherwise, no. Commented Mar 24, 2020 at 15:22
• I see you've had mercy on languages that don't support numbers that require 8 bits, a generous allowance. Any chance it's also alright if our language doesn't support negative numbers? (I don't mean to keep poking at the question, but I'd really love to be able to submit answers in languages not meant for this sort of thing and still have them be reasonably elegant, even if not short!) Commented Apr 24, 2020 at 18:29
• @AviF.S. That's acceptable. The other was not, as that would make the challenge extremely trivial. Commented Apr 24, 2020 at 18:36

x86-16 machine code, 8 bytes

33 D2       XOR  DX, DX         ; clear running sum (in DX)
AN:
E2 FB       LOOP AN             ; loop until CX = 0


Input size in CX, data in [SI]. Output sum in DX.

PHP, 20 9 bytes

array_sum


Try it online!

still wondering if this is not too easy here.. EDIT: thanks to @640KB for saving 11 bytes!

• Your answer could just be array_sum as an anonymous function for 9 bytes. tio.run/… Commented Apr 20, 2020 at 19:50
• Thanks! I didn't know PHP would use the function properly when replacing what it seems to be an undefined constant. I also don't know yet all the rules of the site and would have left the semicolon inside the count too. Commented Apr 21, 2020 at 9:42
• You are allowed to submit an anonymous "callable" function (example function($x) { return$x+1; }) which isn't really any different functionally than a pre-defined / built-in. While sometimes considered "not interesting", if there's a PHP function that answers the challenge then it's valid to submit just that. To note, there are other answers here doing the same (just with shorter built-in function names than PHP of course). Commented Apr 25, 2020 at 15:34

SQL, 21 bytes

SELECT SUM(N) FROM T;


This assumes the numbers to be in table T, in a column named N.

MAWP 1.1, 21 bytes

%@[~@~1A]_1A[%M_1A]%:


Try it!

Input taken as:

number_of_inputs
n1
n2
n3
...
nN


Lua, 57 bytes

print(load("return "..table.concat(arg,('').char(43)))())


Try it online!

Java (JDK), 56 bytes

For once, in this challenge we can't use Java lambdas because its arrow -> contains the - sign!

So i think the shortest way in Java to add N numbers
(unlike using a pre-built function that only adds 2 numbers, which has also been explicitly disallowed by OP)
without using + and -, is this approach:

int f(int...a){return java.util.Arrays.stream(a).sum();}


Try it online!

Scala, 19 bytes

Try it online!

def f(a:Int*)=a.sum


Rust, 29 (maybe 10?) bytes

Since Rust supports signed integers natively we have to use a signed type, but the challenge lets us choose the type beyond that, so I choose i8 in order to save two bytes.

|n:&[i8]|n.iter().sum::<i8>()


Attempt This Online!

Depending on how the rules should be interpreted it could be possible to solve it using only 10 bytes:

|n|n.sum()


Attempt This Online!

by moving the type declarations into the driving code, but that requires the driving code to contain the - character, which I feel is against the rules.

Thunno 2S, 0 bytes

That's right. No bytes. The S flag takes the sum.

Thunno 2, 1 byte

S


1 byte flagless. Built-in for sum.

Desmos, 50 bytes

\sum_{n=1}^{a.\operatorname{length}}a\left[n\right]


Takes input as array a

View it on Desmos

• As I mentioned in a comment on one of your other posts, variable input (the variable a in this case) is not allowed as per meta consensus. With that in mind, something like f(a)=a.total would be a much shorter alternative to what you have currently. Commented May 13, 2023 at 8:41

Red, 4 bytes

:sum


Try it online!

Takes the input as a block (list)

Retina 0.8.2, 79 bytes

O[^,\d]?\d\d*
\d\d*
$* ,[^,1] ^([^,1]1*),$1;
,

\D?(1*);\1

^(\D)?(1*)
$1$.2


Try it online! Link includes test cases. Explanation:

O[^,\d]?\d\d*


Sort the numbers.

\d\d*
$*  Convert the numbers to unary. ,[^,1]  Total the numbers that are less than zero. ^([^,1]1*),$1;


Separate the numbers that are less than zero from those that are not less than zero.

,


Total the numbers that are not less than zero.

\D?(1*);\1


Add the less than zero total to the not less than zero total.

^(\D)?(1*)
$1$.2


Convert to decimal.

Adding nonnegative integers is much easier of course.

\d\d*
$* 1  Try it online! Explanation: Each integer is converted into unary and then the total is converted into decimal. Charcoal, 3 bytes ＩΣＡ  Try it online! Link is to verbose version of code. Explanation:  Ａ Input as an array Σ Take the sum Ｉ Cast to string Implicitly print  Ruby, 18 bytes def f(*a)a.sum end  Try it online! Pushy, 2 bytes S#  Try it online! As you can probably guess, S sums all values on the stack and # outputs this as a number. Javascript (40 characters) x=>eval(x.join(String.fromCharCode(43))) The boring and obvious way to do this, I guess. Javascript (48 characters) x=>x.reduce((a,v)=>[...a,...Array(v)],[]).length A bit lame, but my first attempt. Concatenates arrays of the length of each number, then finds the total length of that array. Burlesque, 6 bytes ps{}ms  Try it online! Takes input as a space separated string from stdin. Explanation: ps # Parse input as block ms # Map and sum result {} # Empty mapping function  C++ (clang), 81 75 bytes [](auto x){decltype(x)r;for(int a:x)r.insert(r.end(),a,0);return r.size();}  I used a generic lambda to avoid the need for #include. It uses a temporary container, to which it appends lists of zeros of needed lengths. The output is the length of the resulting list. Thanks to S.S. Anne for the idea of using decltype! I used auto earlier, and it was worse because I had to clear the list before working with it. Try it online! • Try decltype... Commented Jun 17, 2020 at 20:28 SNOBOL4 (CSNOBOL4), 59 bytes N S =S ' ' CHAR(43) ' ' INPUT :S(N) O OUTPUT =EVAL(0 S) END  Try it online! Rockstar, 66 bytes listen to N R's0 while N cast N let R be with N listen to N say R  Try it here (Code will need to be pasted in, with each input integer on an individual line) ><> (Fish), 23 bytes Takes input in unary, separated by NULL bytes. A bit cheaty, let me know if you think this is valid or no. i:0=?~:0(?v ;nl/  Try it ><> (Fish), 34 bytes Input must be length prefixed. Uses p to modify the source code at runtime to insert + and - instructions. 'V'2,61p0iv :?!v$i $1 >59*91p ;n$/


Try it

• 16 bytes for the first answer, clearing out the whitespace and also fixing the result being off by one
– Jo King
Commented May 1, 2023 at 2:47

minigolf, 39 bytes

1i,ny*_:1<sv,1s/_:,:ny=!ns,,___s,_s,T*_


Attempt This Online!

Regex, 1 byte

x


Try it online! Takes input as space-delimited unary, outputs using the number of matches. This is a standard I/O format for regex that just happens to trivialise this challenge.

Swift, 87 bytes

The xor & carry based approach. The return type of a function in Swift is defined using the arrow like f() -> Int with a "minus" character, so my function needs to print the value and not return it :-) (that way actually 5 bytes shorter than returning the result)

func s(_ a:[Int]){var r=0;for var c in a{while 0 != c{(r,c)=(r^c,(r&c)<<1)}};print(r)}


Gema, 35 characters

<N>=@set{s;@add{${s;};$0}}
?=
\Z=$s  Takes an input string with anything separated numbers. Sample run: bash-5.2$ gema '<N>=@set{s;@add{${s;};$0}};?=;\Z=$s' <<< '-64, -64, 16' -112  jq, 3 characters add  Takes the input as array. Sample run: bash-5.2$ jq 'add' <<< '[-64, -64, 16]'
-112


Java, 15 bytes

Math.addExact()


You give it your two variables as parameters

• Welcome to CGCC! We don't allow predefined variables as input, however you can submit just the function itself as Math.addExact as a solution instead, since it fulfills the challenge specifications.
– Jo King
Commented Mar 26, 2020 at 3:25