9
\$\begingroup\$

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

Rules

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.

\$\endgroup\$
6
  • \$\begingroup\$ 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? \$\endgroup\$
    – AviFS
    Commented Mar 24, 2020 at 2:26
  • 2
    \$\begingroup\$ @AviF.S., that would not be a valid solution to the challenge. \$\endgroup\$
    – Shaggy
    Commented Mar 24, 2020 at 13:37
  • \$\begingroup\$ @Avi If you create another function that does the rest of the adding, then yes. Otherwise, no. \$\endgroup\$
    – S.S. Anne
    Commented Mar 24, 2020 at 15:22
  • \$\begingroup\$ 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!) \$\endgroup\$
    – AviFS
    Commented Apr 24, 2020 at 18:29
  • \$\begingroup\$ @AviF.S. That's acceptable. The other was not, as that would make the challenge extremely trivial. \$\endgroup\$
    – S.S. Anne
    Commented Apr 24, 2020 at 18:36

57 Answers 57

1
2
1
\$\begingroup\$

x86-16 machine code, 8 bytes

33 D2       XOR  DX, DX         ; clear running sum (in DX)
        AN: 
AD          LODSW               ; load next value into AX
03 D0       ADD  DX, AX         ; add AX to running sum
E2 FB       LOOP AN             ; loop until CX = 0
C3          RET                 ; return to caller

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

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

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!

\$\endgroup\$
3
  • \$\begingroup\$ Your answer could just be array_sum as an anonymous function for 9 bytes. tio.run/… \$\endgroup\$
    – 640KB
    Commented Apr 20, 2020 at 19:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kaddath
    Commented Apr 21, 2020 at 9:42
  • \$\begingroup\$ 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). \$\endgroup\$
    – 640KB
    Commented Apr 25, 2020 at 15:34
1
\$\begingroup\$

SQL, 21 bytes

SELECT SUM(N) FROM T;

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

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

MAWP 1.1, 21 bytes

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

Try it!

Input taken as:

number_of_inputs
n1
n2
n3
...
nN
\$\endgroup\$
1
\$\begingroup\$

Lua, 57 bytes

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

Try it online!

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

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!

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

Scala, 19 bytes

Try it online!

def f(a:Int*)=a.sum
\$\endgroup\$
1
\$\begingroup\$

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.

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

Thunno 2 S, 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.

Screenshots

Screenshot 1 Screenshot 2

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

Desmos, 50 bytes

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

Takes input as array a

View it on Desmos

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Aiden Chow
    Commented May 13, 2023 at 8:41
0
\$\begingroup\$

Red, 4 bytes

:sum

Try it online!

Takes the input as a block (list)

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

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.

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

Charcoal, 3 bytes

IΣA

Try it online! Link is to verbose version of code. Explanation:

  A Input as an array
 Σ  Take the sum
I   Cast to string
    Implicitly print
\$\endgroup\$
0
\$\begingroup\$

Ruby, 18 bytes

def f(*a)a.sum end

Try it online!

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

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.

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

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.

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

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
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ Try decltype... \$\endgroup\$
    – S.S. Anne
    Commented Jun 17, 2020 at 20:28
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 59 bytes

N S =S ' ' CHAR(43) ' ' INPUT :S(N)
O OUTPUT =EVAL(0 S)
END

Try it online!

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

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)

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

><> (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

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

minigolf, 39 bytes

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

Attempt This Online!

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

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.

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

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)}
\$\endgroup\$
0
\$\begingroup\$

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

Try it online! / Try all test cases online!

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

jq, 3 characters

add

Takes the input as array.

Sample run:

bash-5.2$ jq 'add' <<< '[-64, -64, 16]'
-112

Try it online! / Try all test cases online!

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

Java, 15 bytes

Math.addExact()

You give it your two variables as parameters

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Jo King
    Commented Mar 26, 2020 at 3:25
1
2

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.