33
\$\begingroup\$

Goal:

Given two natural numbers (integers from 0 to infinity), output a number that is not the sum of those numbers, but is a natural number.

Example solutions (TI-Basic):

  • A+B+1

  • not(A+B)

Invalid solutions:

  • A+B-1 (for inputs 0,0, it returns -1, which is not natural)

  • "ABC" (ABC is not a number)

Notes:

  • The output must always be a sum of two natural numbers (which is actually just a natural number)

  • -1, undefined, infinity, NaN and Error messages are not natural numbers. For our purposes, 0 is natural (although not all mathematicians agree).

\$\endgroup\$
  • 1
    \$\begingroup\$ Maybe we take the numbers as strings and output as a string? \$\endgroup\$ – xnor Feb 13 '17 at 3:01
  • 1
    \$\begingroup\$ Can the output have leading zeroes? \$\endgroup\$ – Cows quack Feb 13 '17 at 8:27
  • 1
    \$\begingroup\$ I presume overflows need to be taken into account, so the result of 2^32 -1 and 2 should not be negative, right? \$\endgroup\$ – adrianmp Feb 13 '17 at 12:28
  • 1
    \$\begingroup\$ Just a small remark because I like to pay attention to useless details: 0 is not a natural number. If you change the first sentence to "Given two non-negative integers ...", there won't be any useless detail left for me to comment on. :) \$\endgroup\$ – peech Feb 13 '17 at 17:11
  • 6
    \$\begingroup\$ @peech This is not true. 0 is considered a natural number under some definitions. You cannot see it because it has been deleted but there has been an extensive conversation on this matter. \$\endgroup\$ – Sriotchilism O'Zaic Feb 13 '17 at 17:16

76 Answers 76

36
\$\begingroup\$

RProgN, 4 3 1 Byte

Crossed out 4 is still 4 ;(

E

The most simple of solutions, compares if A and B are equal. Pushes true, which RProgN sees as 1, if they are the same, or false aka 0 otherwise.

Test Cases

0+0 = 1
1+0 = 0
0+1 = 0
1+1 = 1

Try it online!

\$\endgroup\$
  • 22
    \$\begingroup\$ I just went down the rabbit hole with your crossed out link. I rate it <s>4</s>/4 \$\endgroup\$ – Rohan Jhunjhunwala Feb 14 '17 at 1:10
  • 2
    \$\begingroup\$ @RohanJhunjhunwala hold my 4, I'm going in \$\endgroup\$ – Albert Renshaw Feb 14 '17 at 3:20
  • 4
    \$\begingroup\$ ̶4̶ <-- u+0336 (combining character) may be a better way to do it \$\endgroup\$ – Albert Renshaw Feb 14 '17 at 3:23
  • 3
    \$\begingroup\$ PSA: codegolf.stackexchange.com/questions/48100/… Original crossed out thing \$\endgroup\$ – Christopher Feb 27 '17 at 23:50
  • 3
    \$\begingroup\$ @AlbertRenshaw the old PPCG crossed-out-4-a-roo? \$\endgroup\$ – Rɪᴋᴇʀ Mar 7 '17 at 16:36
16
\$\begingroup\$

Python, 13 bytes

[(0,0)].count

Try it online! Takes input as a tuple.

Using an object method for the function avoids the boilerplate of a lambda.

lambda a,b:a-~b   # 15 bytes

Here, the idea is to map (0,0) to 1 and everything else to 0. Since only 0+0 gives a sum of 0 among natural numbers, that always avoids matching the sum.

If one could output a Boolean here, which I find shady, a byte could be saved as

(0,0).__ge__

This checks if the input tuple is at most (0,0), which is only true for (0,0). In Python, True==1 and False==0. Even more shadily, outputting via exit code and treating that as a Python Boolen would save two bytes:

[(0,0)].pop

If string I/O is allowed and leading zeroes are OK, there's the 8-byte solution

'1'.join

This concatenates a1b, which is always bigger than a+b.

\$\endgroup\$
  • 1
    \$\begingroup\$ int.__eq__ for 10 bytes \$\endgroup\$ – Blue Feb 13 '17 at 12:48
  • \$\begingroup\$ @muddyfish Yeah, I saw suever's answer too, didn't think of using equality. It returns a bool though, which I think is iffy on a challenge that asks for a number output. \$\endgroup\$ – xnor Feb 13 '17 at 13:32
  • 1
    \$\begingroup\$ IMO if it swims like a number and quacks like a number, it's reasonable to assume it's a number. \$\endgroup\$ – CalculatorFeline Jun 19 '17 at 17:47
15
\$\begingroup\$

Retina, 3 bytes

 
1

Try it online!

(The first line has a space before the newline. Stack Exchange isn't very good at showing trailing whitespace.)

Input is the numbers in decimal, separated by a space (e.g. 12 34). This program just changes the space to a 1, creating a number too large to be the sum of the input numbers (it necessarily has at least 2 more digits than either, and adding two numbers produces an output with no more than 1 digit more than the larger input).

\$\endgroup\$
  • 2
    \$\begingroup\$ 0 0 should also work. \$\endgroup\$ – Dennis Feb 13 '17 at 3:59
  • 1
    \$\begingroup\$ @Dennis: I was wondering about that. 010 is considered to be an integer via basically all integer parsers. I can see a potential argument that 0 8 is invalid on the basis that 018 is considered invalid octal via some integer parsers (although it's considered to be decimal 18 by others). Note that this program is quite happy to handle leading zeroes in the input, treating them as decimal; and I've written programs that output leading zeroes for other questions without people seeing a problem. Is there a relevant meta post on the subject? \$\endgroup\$ – user62131 Feb 13 '17 at 4:04
  • 2
    \$\begingroup\$ Or . 1 if you don't want to return leading zeros yourself. \$\endgroup\$ – Martin Ender Feb 13 '17 at 14:48
  • \$\begingroup\$ @MartinEnder > but is a natural number \$\endgroup\$ – wizzwizz4 Feb 14 '17 at 19:43
  • \$\begingroup\$ @wizzwizz4 I'm not following. \$\endgroup\$ – Martin Ender Feb 14 '17 at 19:44
13
\$\begingroup\$

MATL, et al. 1 byte

=

Accepts two natural numbers as inputs and compares them. If they are equal, the output is 1 and if they not equal the output is 0. This is the same approach as @ATaco's solution.

\$\endgroup\$
  • 3
    \$\begingroup\$ The = solution also works in Jelly for 1 byte. I thought I'd mention it in the comments as it doesn't seem worth creating a separate answer for the trivial solution. \$\endgroup\$ – user62131 Feb 13 '17 at 3:01
  • \$\begingroup\$ @ais523 Updated to include that. Thanks. \$\endgroup\$ – Suever Feb 13 '17 at 3:07
  • 2
    \$\begingroup\$ Also in Stacked. Try it online! \$\endgroup\$ – Conor O'Brien Feb 13 '17 at 16:36
  • \$\begingroup\$ Can you add APL and J? \$\endgroup\$ – Adám Feb 14 '17 at 18:08
  • \$\begingroup\$ @Adám Sure thing. Do you have a TIO link that I can link to? \$\endgroup\$ – Suever Feb 14 '17 at 18:19
10
\$\begingroup\$

Javascript, 10 bytes

x=>y=>!x+y

Takes 2 numbers using currying syntax like so:

(x=>y=>!x+y)(0)(0) // 1
\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – DJMcMayhem Feb 13 '17 at 22:04
  • \$\begingroup\$ Thanks =) I've been reading challenges for a while, just trying to find a good place to start. \$\endgroup\$ – Malivil Feb 14 '17 at 13:52
9
\$\begingroup\$

Vim, 3 bytes/keystrokes

<C-a>gJ

Try it online!

Note that <C-a> is actually ctrl-a, which represents byte 0x01.

I love it when vim (which isn't even a programming language) can compete with golfing languages. :) Input comes in this format:

a
b

This simply increments the first number by one (This is the <C-a> part) and then joins the string representations of the two numbers together. As far as I can tell, this should never result in the sum.

\$\endgroup\$
9
\$\begingroup\$

Brain-Flak, 8 bytes

({}{}())

Try it online!

This is the most readable brain-flak answer I have ever written. :)

Explanation:

(      )    # Push the sum of all the following:
 {}         #   The first input
   {}       #   The second input
     ()     #   and one

Alternate solutions (also 8 bytes):

({}[]{})    # Sum + 1
([]{}{})    # Sum + 2

There's a bunch of other solutions that only work with positive numbers:

(<{}>{})
({}<{}>)
({{}}())
({{}()})
({{}}[])
({{}[]})
\$\endgroup\$
7
\$\begingroup\$

Jelly, 2 bytes

+‘

The + adds the two inputs together then the ' increments the answer by one

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This answer makes me jelly. \$\endgroup\$ – MD XF May 20 '17 at 1:56
  • \$\begingroup\$ I bet it does :P \$\endgroup\$ – Christopher May 20 '17 at 19:34
  • \$\begingroup\$ Technically this is not (a+b)+1 but a+(b+1) because the dyad-monad chain fG is treated as f(a, G(b)). In this case it's the same thing but technically how it works is different :P \$\endgroup\$ – HyperNeutrino May 17 '18 at 12:29
6
\$\begingroup\$

TI-Basic, 3 bytes

not(max(Ans

Alternative solutions:

10^(sum(Ans         3 bytes @DestructibleWatermelon
not(sum(Ans         3 bytes
1+sum(Ans           4 bytes
Input :X=Y          5 bytes @ATaco
Input :X+not(Y      6 bytes
Input :not(X+Y      6 bytes
Input :10^(X+Y      6 bytes
Input :X+Y+1        7 bytes
Input :not(max(X,Y  7 bytes
Ans(1)=Ans(2        8 bytes
Ans(1)+not(Ans(2    9 bytes
not(Ans(1)+Ans(2    9 bytes

It's interesting that you made the question's examples in TI-Basic, but you forgot the shorter A=B (or maybe it was up to us to find out?)

\$\endgroup\$
  • 1
    \$\begingroup\$ Nobody likes it when the OP posts a super short solution in the question, making it hard to beat. \$\endgroup\$ – mbomb007 Feb 13 '17 at 20:20
  • \$\begingroup\$ @mbomb007 I suppose, but those were only snippets and not full programs. Adding Prompt A,B: to them brings the byte count to eight bytes each. \$\endgroup\$ – Timtech Feb 13 '17 at 22:14
  • 1
    \$\begingroup\$ @Timtech Exactly. I didn't want to give good answers as examples, I just wanted examples. \$\endgroup\$ – Julian Lachniet Feb 15 '17 at 11:57
  • \$\begingroup\$ @JulianLachniet I understand and appreciate that :) \$\endgroup\$ – Timtech Feb 15 '17 at 15:32
6
\$\begingroup\$

Brachylog, 2 bytes

+<

Try it online!

Explanation

+     The sum of the elements in the Input...
 <    ...is strictly less than the Output
      (implicitely label the output with an integer respecting this constraint)

This will always result in A+B+1, if Input = [A, B].

\$\endgroup\$
5
\$\begingroup\$

Mathematica, 5 bytes

1+##&

Outputs the sum of the two arguments plus 1. For example, 1+##&[2,5] yields 8.

(Side note: Binomial almost works, although Binomial[1,0]=1 and Binomial[4,2]=6 are counterexamples; I think they're the only counterexamples, though.)

\$\endgroup\$
  • \$\begingroup\$ Pochhammer seems to be one better than Binomial. As far as I can tell only 1,0 fails. \$\endgroup\$ – Martin Ender Feb 13 '17 at 12:05
  • \$\begingroup\$ Ah, and KroneckerDelta works for all inputs (being the equivalent of the equality check in some of the esolangs). It's actually shorter to reimplement as Boole[#==#2]&, but I assume you were looking for a built-in that works as is. \$\endgroup\$ – Martin Ender Feb 13 '17 at 12:08
5
\$\begingroup\$

PHP, 17 bytes

<?=1-join($argv);

Run like this:

echo '<?=1-join($argv);' | php -- 0 0
> 1

Explanation

This just concatenates the arguments. The first argument (script name) contains -. So that results in a negative number, which I negate with the minus sign. Then I add 1, just in case the first input number is a 0 (0123 = 123).

\$\endgroup\$
5
\$\begingroup\$

Perl 6, 4 bytes

!*+*

A lambda (formed by Whatever-currying), that adds the boolean inverse (1 or 0) of the first argument to the second argument.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Turtlèd, 12 bytes

makes very large numbers

'1?:?:[1'0l]

Try it online!

Explanation:

'1                write one on the starting grid square
  ?:?:            take a number, move right that many (repeat)
      [1   ]      while not on a grid square with a one on it
        '0l       put a zero on that square, move left
[implicit output of grid]

It thus outputs 10**(x+y).

\$\endgroup\$
4
\$\begingroup\$

PHP, 19 bytes

1<?=max([0]+$argv);
\$\endgroup\$
4
\$\begingroup\$

Java (OpenJDK 9), 10 bytes

a->b->a-~b

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ With currying, you can spare a byte: a->b->a-~b. Also works with Java 8, any edition (so there's no need to specify the OpenJDK 9) \$\endgroup\$ – Olivier Grégoire Feb 14 '17 at 9:20
  • \$\begingroup\$ @OlivierGrégoire Java has started to look like JS now >_> \$\endgroup\$ – Cows quack Feb 14 '17 at 9:34
  • \$\begingroup\$ @KritixiLithos Well... we had a hint this would happen for years: JavaScript ;-) \$\endgroup\$ – Olivier Grégoire Feb 14 '17 at 10:01
  • \$\begingroup\$ @KritixiLithos The specification for Java 9 has a section on 'ECMAScript 6 Compliance'. \$\endgroup\$ – Pavel Feb 14 '17 at 15:33
  • \$\begingroup\$ @OlivierGrégoire Yeah, but this submission was generates automatically, which is why the 9 was added. \$\endgroup\$ – Pavel Feb 14 '17 at 15:33
3
\$\begingroup\$

SmileBASIC, 4 bytes

!A+B

not(A)+B
1+1=2 -> !1+1 -> 0+1=1
0+1=1 -> !0+1 -> 1+1=2

\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, how does this support 2+1? \$\endgroup\$ – ATaco Feb 13 '17 at 1:53
  • 2
    \$\begingroup\$ 2+1=3 -> !2+1 -> 0+1=1 \$\endgroup\$ – 12Me21 Feb 13 '17 at 1:54
3
\$\begingroup\$

R, 13 bytes

sum(scan()+1)

Thanks to Jonathan Allan for his inputs !

\$\endgroup\$
  • \$\begingroup\$ @JonathanAllan : You're right, I changed my answer. Thanks ! \$\endgroup\$ – Frédéric Feb 13 '17 at 7:06
  • \$\begingroup\$ OK, pretty sure 00 is the same as 0 though, maybe sep="1"? \$\endgroup\$ – Jonathan Allan Feb 13 '17 at 7:08
  • \$\begingroup\$ @JonathanAllan : damnit ! Thanks again ! \$\endgroup\$ – Frédéric Feb 13 '17 at 7:38
  • \$\begingroup\$ Looking at the Tips For Golfing In R it seems scan() should be fine, accepting a vector input, like this. But we can do one byte better with cat(sum(scan()+1)). Maybe there is shorter? \$\endgroup\$ – Jonathan Allan Feb 13 '17 at 7:54
  • 1
    \$\begingroup\$ With the cat it is a full program, the alternative would be an unnamed function for the same byte cost function(a,b)a+b+1 \$\endgroup\$ – Jonathan Allan Feb 13 '17 at 8:10
3
\$\begingroup\$

05AB1E, 1 byte

Q

Works the same as the RProgN answer.

Checks if a and b are the same. If so, print 1. Otherwise, print 0

Try it online!

\$\endgroup\$
  • 3
    \$\begingroup\$ ¢ ( a.count(b) ) should also work for 1 byte. \$\endgroup\$ – Riley Feb 13 '17 at 16:54
  • \$\begingroup\$ @Riley you could post that as your own answer. \$\endgroup\$ – Okx Feb 13 '17 at 17:48
  • 2
    \$\begingroup\$ It's not different enough to needs it's own answer. I thought we could just combine both 1 byte solutions into one answer. \$\endgroup\$ – Riley Feb 13 '17 at 17:49
3
\$\begingroup\$

C 26 24 19 bytes

f(c,d){return!c+d;}

Ungolfed version:

int f(int c,int d)
{
   return !c+d; 
}

I hope I got the specification right. Can definitely be shortened!?

@Pavel Thanks for saving 2 bytes

@Neil Thanks for your input.

\$\endgroup\$
  • 1
    \$\begingroup\$ Do you need () around !c+d? \$\endgroup\$ – Pavel Feb 13 '17 at 7:48
  • \$\begingroup\$ @Pavel You're right, brackets were useless, updated! \$\endgroup\$ – Abel Tom Feb 13 '17 at 8:07
  • 2
    \$\begingroup\$ Not 100% sure but I think you can remove the space in your return, like return!c+d; \$\endgroup\$ – Metoniem Feb 13 '17 at 8:50
  • 1
    \$\begingroup\$ Lose the return and instead assign it with something like c+=!d \$\endgroup\$ – Ahemone Feb 13 '17 at 10:22
  • 1
    \$\begingroup\$ @AlbertRenshaw It's not something I'd rely on for portability but here are a couple of examples. I can't get it to work offline and it seems that it needs to be assigned to a non argument variable on TIO.. tio.run/nexus/… \$\endgroup\$ – Ahemone Feb 14 '17 at 6:44
3
\$\begingroup\$

MATLAB / Octave, 3 bytes

@eq

Accepts two inputs and checks for equality and yields 1 if they are equal and 0 otherwise.

Online Demo

\$\endgroup\$
  • 4
    \$\begingroup\$ Shouldn't this be @eq? That returns a function handle which can be used to evaluate to the desired function, while just eq is meaningless. \$\endgroup\$ – Sanchises Feb 13 '17 at 8:16
  • \$\begingroup\$ @Sanchises I've seen many answers go both ways: codegolf.stackexchange.com/questions/106149/compute-the-median/…. I'm not actually sure which is preferred. \$\endgroup\$ – Suever Feb 13 '17 at 13:30
  • \$\begingroup\$ Hmmm. I should think this is more like a snippet, while an @ turns it into a valid language construct. But maybe I'm just being pedantic. \$\endgroup\$ – Sanchises Feb 13 '17 at 13:58
3
\$\begingroup\$

brainfuck, 12 bytes

Simple solution that outputs A+B+1.

,>,[-<+>]<+.

Try it online

\$\endgroup\$
  • \$\begingroup\$ Alternate answer (12 bytes): ,>,[-<++>]<. \$\endgroup\$ – Julian Lachniet Feb 13 '17 at 20:37
  • \$\begingroup\$ @JulianLachniet will that output A+2B? \$\endgroup\$ – george Mar 8 '17 at 13:37
  • \$\begingroup\$ A+2B hacked when B=0 \$\endgroup\$ – l4m2 Dec 15 '17 at 18:48
  • \$\begingroup\$ @mbomb007 I'm saying the ,>,[-<++>]<. solution \$\endgroup\$ – l4m2 Dec 18 '17 at 21:04
  • \$\begingroup\$ @JulianLachniet Yeah, that's not a valid answer because A+2B for input B=0, gives A. \$\endgroup\$ – mbomb007 Dec 18 '17 at 22:40
3
\$\begingroup\$

dc, 5 bytes

?1n+n

Try it online!

Input: Two natural numbers separated by a space on stdin.

Output: The digit 1 immediately followed by the sum of the two numbers, which is a number larger than the sum of the two numbers.

Example:

Input: 222 333

Output: 1555

\$\endgroup\$
3
\$\begingroup\$

PHP, 13 bytes; (17 REPL-less)

!max($argv)+0

Examples

[0,0] -> 1
[0,1] -> 0
[1,0] -> 0

For those without REPL use

<?=!max($argv)+0;

and run using

echo '<?=!max($argv)+0;' | php -- 0 0
\$\endgroup\$
  • \$\begingroup\$ This answer is not valid because it doesn't output anything \$\endgroup\$ – aross Feb 14 '17 at 8:57
  • \$\begingroup\$ @aross If boolean cast was problem I updated my answer \$\endgroup\$ – mleko Feb 14 '17 at 11:29
  • \$\begingroup\$ Yes, you addressed both problems. The output would be true/false, not 1/0. Also, REPL :) \$\endgroup\$ – aross Feb 14 '17 at 11:46
3
\$\begingroup\$

Cubix, 9 8 bytes

u-~OII/@

Explanation

Expanded, this answer looks like this:

    u -
    ~ O
I I / @ . . . .
. . . . . . . .
    . .
    . .

The order of the instructions that are executed is II~-O@

II~-O@
I      # First input
   -   # Minus
 I~    # NOT(second input)
    O  # Output as integer
     @ # End program

Tested for all combinations of inputs where both are in the range 0-100.

Try it here.

\$\endgroup\$
3
\$\begingroup\$

HODOR, 40 bytes (non-competing)

This is probably the shortest program Hodor's ever written!

This is what happens when you have nothing to do for a 2-week school holiday: produce a bunch of really easily coded joke languages that do absolutely nothing. Yay for school holidays!!!

Walder
Hodor?!
Hodor?!
Hodor HODOR!
HODOR!!!

Walder was Hodor's original name and so is needed to begin the program.

Hodor?! takes either a number from STDIN or a single character and sets the accumulator to the input

Hodor add 1 to the accumulator

HODOR! outputs the accumulator as a number

HODOR!!! kills Hodor! Noooooo!

This is the pseudo code:

Take input
Take input
Add 1 to sum(inputs)
Output value
\$\endgroup\$
  • 1
    \$\begingroup\$ I think you need to come up with a different name for your language unless this is an interpreter of the pre-existing Hodor language created in 2015, which I'm fairly certain this isn't. \$\endgroup\$ – mbomb007 Apr 5 '17 at 13:52
  • \$\begingroup\$ @mbomb007 No that isn't mine but there are languages with duplicate names. I know of two called 7 on this site (I just can't find them at the moment) \$\endgroup\$ – caird coinheringaahing Apr 5 '17 at 13:58
  • 1
    \$\begingroup\$ I'm pretty sure there's only one called 7, and it's this one. You could change the capitalization of the title for an easy fix, something like HODOR. \$\endgroup\$ – mbomb007 Apr 5 '17 at 13:58
  • \$\begingroup\$ @mbomb007 mine is Hodor and theirs is hodor so that could be enough? \$\endgroup\$ – caird coinheringaahing Apr 5 '17 at 14:01
  • \$\begingroup\$ No, theirs has a capital H, as you can see from every time he uses the language name on his website. \$\endgroup\$ – mbomb007 Apr 5 '17 at 14:01
2
\$\begingroup\$

Billiards, 11 characters = 17 bytes

⇲
⇲
+
1
+
↥

Implements x + y + 1. Pretty elementary. It takes the inputs on two separate lines. (By the way, the language was modified slightly after the challenge, but only to suppress the prompt from inputting, not sure if this answer is still valid).

7 characters = 11 bytes, non-competing

This one is shorter but only possible after a new update of the language:

⇲
⇲
=
$

This uses x == y, which was shamelessly stolen from @ATaco's RProgN solution [ hope you don't mind (: ]. The $, on exit, outputs how many balls passed over it.

\$\endgroup\$
  • \$\begingroup\$ slightly after the language typo, also I think when the language had the input prompt, it would still be valid. I don't think output standards are that strict, and non-suppressible outputs are allowed I think \$\endgroup\$ – Destructible Lemon Feb 13 '17 at 2:12
  • \$\begingroup\$ @DestructibleWatermelon Oh, that was a weird typo. Thanks for the clarification. I actually modified the language, then came and saw the challenge, so I decided to add that note in there in case anyone decided to be picky about checking when the push was made to GitHub, but thank you for clarifying that. \$\endgroup\$ – HyperNeutrino Feb 13 '17 at 2:14
2
\$\begingroup\$

Carrot, 3 bytes

1$$

Try it online!

(input is newline separated)

Similar to ais529's answer, except this takes care of leading zeroes. This outputs 1 concatenated with both the input numbers. So 5\n6 outputs 156.

Alternate solutions:

6 bytes

1$$^*1

Same thing as the above solution, but concatenates it with a copy of itself. So 5\n6 becomes 156 which then becomes 156156.

8 bytes

#^A +1+ 

(note the trailing space, the input is space separated this time)

#^A +1+ 
#^                        //gets all of the input
  A                       //convert it an array splitting on spaces
   +1                     //add 1 to each of the array's elements
     +                    //sums up all the elements in the array
\$\endgroup\$
2
\$\begingroup\$

C#, 32 bytes

Golfed

int i(int x,int y){return++x+y;}

Ungolfed

int i (int x, int y)
{
    return ++x+y; //Returns X+Y+1
}
\$\endgroup\$
2
\$\begingroup\$

Ruby, 9 bytes (8 + -n flag)

p~/1/||1

Input 2 integers separated by comma or space or semicolon or just whatever.

Explanation:

If the input string does not contain any '1', output '1' (since the sum can't be one).

If the input string is 3 characters long, the position of the first one can be 0 or 2, the sum can't be zero, and only '1 1' has sum two (but the first '1' is at position zero).

If the input string is longer, we don't care, the sum will be bigger than one and bigger than the position of the first '1'.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.