# 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 marked * are only required if your integer type supports each partial sum (if any) and the final result.

• 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? – Avi F. S. Mar 24 at 2:26
• @AviF.S., that would not be a valid solution to the challenge. – Shaggy Mar 24 at 13:37
• @Avi If you create another function that does the rest of the adding, then yes. Otherwise, no. – S.S. Anne Mar 24 at 15:22

# Wolfram Language (Mathematica), 2 bytes

Tr finds the trace of the matrix or tensor list

Tr


Try it online!

• @a'_' As far as I remember, it sums the elements on the diagonal of a matrix, or, if it's actually a vector, just sums everything. – my pronoun is monicareinstate Mar 23 at 5:33

# Python 2, 979593 88 bytes

Solution that doesn't use the built-in sum, eval or exec:

-2 bytes thanks to @JonathanAllan!

x=y=1
for i in input():x<<=i*(i>0);y<<=abs(i*(i<0))
print" ~"[x<y],len(bin(x/y|y/x)[3:])


Try it online!

Input: a comma separated list of numbers, from stdin.
Output: the sum is printed to stdout. If the sum is negative, the ~ sign is used instead of - due to source code restriction.

How: Let $$\p\$$ be the sum of all positive numbers in the list, and $$\n\$$ be the magnitude of the sum of all negative numbers. Then the sum of the list is $$\p-n\$$.

Let $$\x=2^p\$$ and $$\y=2^n\$$, then $$\\frac xy=2^{p-n}\$$.

Thus if the sum is positive (aka $$\x>y\$$), we can calculate the sum by counting the number of zeros in the binary representation of $$\\frac xy\$$. Otherwise, we can calculate the magnitude of the sum as the number of zeros in the binary representation of $$\\frac yx\$$.

• Lovely. It would be nice to see an explanation. – S.S. Anne Mar 22 at 22:38
• x<<=i*(i>0);y<<=abs(i*(i<0)) saves two over using max and min. – Jonathan Allan Mar 22 at 23:35

# JavaScript (ES6), 21 bytes

Takes input as an array of integers.

a=>eval(a.join\x2B)


Try it online!

# JavaScript (ES6), 40 bytes

Takes input as an array of integers.

a=>a.reduce(g=(x,y)=>y?g(x^y,(x&y)*2):x)


Try it online!

• Amazing formula! Waiting for an explanation on why this will eventually terminate. – Surculose Sputum Mar 22 at 22:58
• @SurculoseSputum It is quite similar to a binary addition by hand: the XOR does the addition without the carries which are processed separately by the doubled AND. The recursion stop when there's no more carry. It is described here for instance, and some other variants can be found here. – Arnauld Mar 22 at 23:37

# APL (dzaima/APL), 2 bytesSBCS

Anonymous tacit prefix function

1⊥


Try it online!

Simply evaluates a "digit" list in base 1.

• Absolutely brilliant! How ever did it occur to you? The best part is it's no longer than the normal way. Replacing +/ with 1⊥ in all golfed APL has to be the best way of obfuscating and impressing people ever... – Avi F. S. Mar 22 at 22:16
• @AviF.S. It is a common trick in tacit APL due to it being a dyadic function application rather than a monadic one which cannot be composed with a function on its left. – Adám Mar 22 at 22:19
• Whoops. Now that it's documented, I feel silly acting like it was so novel... But I do still think it's a brilliant trick! – Avi F. S. Mar 22 at 22:22

# Japt, 1 byte

Trivial challenges get trivial solutions!

x


Try it

# Python 2/Python 3, 3 bytes

sum

• Also works in Haskell, and many other languages I'm sure. – 79037662 Mar 22 at 23:03

# W, 1 bytes

Takes input as a list and... just a summation function... :-)

J


# Wj, 0 bytes

Haha, even more cheaty! The j flag automatically evaluates the J command at the end of the source code.




# C (gcc), 504543 38 bytes

f(s,e)char**s;{s=s<e?&f(&s[1])[*s]:0;}


-7 bytes thanks to @S.S. Anne
-5 bytes thanks to @Bubbler

Takes for input start and end pointers. It uses the fact that the address of &a[b] equals a+b. Other than that, even I am a bit confused as to how this works.

Try it online!

• Nicely done, but fails on multiple test cases, like 0, 3, 0 and 7, 0, 7 and 0, 9, 10. – S.S. Anne Mar 22 at 23:50
• You're right, let me see if I can fix that... – dingledooper Mar 22 at 23:58
• Nice. 45 bytes using return hack. – S.S. Anne Mar 23 at 0:16
• 43 bytes using the brackets to avoid parentheses. – S.S. Anne Mar 23 at 15:34
• 38 bytes with even more pointer abuse (and 64-bit ints, because TIO runs on a 64-bit machine.) – Bubbler Mar 24 at 0:11

# Jelly, 1 byte

S


A built-in monadic atom which given a list yields the sum.

Try it online!

No built-in, 2 bytes:

ḅ1


Converts from base one to an integer.

Try it online!

# Keg, -hr, 2 bytes

÷⅀


Try it online!

The joys of not having implemented lists properly! Simply item split and summate. Essentially uses a sum function, so no imaginary points for me.

# Bash + GNU utilities, 57 bytes

printf %.f $(bc -l<<<"99*l(e(sed 's@ @/99)*e(@g'/99))")  Try it online! Reads space-separated integers from stdin, and writes the output to stdout. This applies the exponential function to each integer, multiplies the results, and then takes the natural logarithm of the product. I need to scale the input numbers (and then "unscale" the result) so as not to overflow the exponentials on some of the starred test examples (that's what the 99* and /99 are doing there). # Pyth, 1 byte s  Try it online! ### Explanation s(Q) (Q) : Implicit evaluated input s : Sum the input  # C (gcc), 94 bytes x,c;n(a,b){for(;b;b=x*2)x=a&b,a^=b;x=a;}f(a,t)int*a;{for(c=1;c<t;c=n(c,1))*a=n(*a,a[c]);c=*a;}  A non-trivial golfed reference implementation. I realized I had worded myself out of an answer when I couldn't even use + or - for a counter variable. Try it online! # GERMAN, 141 bytes EINGABESCHLEIFENANFANGSUBTRAKTIONRECHTSEINGABESCHLEIFENANFANGSUBTRAKTIONRECHTSADDITIONLINKSSCHLEIFENENDELINKSSCHLEIFENENDERECHTSRECHTSAUSGABE  • i'm disappointed this is just another brainfuck derivative rather than some complex combination of german words – Jo King Mar 23 at 0:22 • @JoKing I am sorry to disappoint you. Unfortunately, programming languages not based on English are quite rare ... – Jonathan Frech Mar 23 at 1:52 # Python 2, 3 bytes sum  Built in function which does the job Try it online With no built-in, 39 bytes: lambda a:eval(a.replace(',',chr(43)))  Try it online! Note: a single value may be represented as a singleton list (meta) # Racket, 133 bytes (define(f a[s 0])(if(null? a)s(let([c(car a)])(if(= 0 c)(f(cdr a)s)(f(cons((if(> 0 c)add1 sub1)c)(cdr a))((if(> 0 c)sub1 add1)s))))))  Try it online! Well... # PHP, 20 bytes <?=array_sum($argv);


Try it online!

still wondering if this is not too easy here..

sum


calculates the sum of a list

## 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 Mar 26 at 3:25

# 05AB1E, 1 bytes

O


Input as a list.

Slightly less boring:

1β


Explanation:

O   # Sum the (implicit) input-list
# (and output the result implicitly)

1β  # Convert the (implicit) input-list to base-1
# (and output the result implicitly)


# Red, 4 bytes

:sum


Try it online!

Takes the input as a block (list)

s/ /\x2b/g;$_=eval  Try it online! Reads a space separated list of numbers from STDIN, writes the sum to STDOUT. # perl -MList::Util=sum -alpe, 8 bytes $_=sum@F


Try it online!

$args-join[char]0x2b|iex  Try it online! -36 bytes thanks to @mazzy • you can to save some bytes Try it online! – mazzy Mar 23 at 14:15 • @mazzy much thanks! – Wasif Hasan Mar 23 at 15:41 # 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!