44
\$\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\$
10
  • 1
    \$\begingroup\$ Maybe we take the numbers as strings and output as a string? \$\endgroup\$
    – xnor
    Feb 13, 2017 at 3:01
  • 1
    \$\begingroup\$ Can the output have leading zeroes? \$\endgroup\$
    – user41805
    Feb 13, 2017 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, 2017 at 12:28
  • 3
    \$\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, 2017 at 17:11
  • 9
    \$\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\$
    – Wheat Wizard
    Feb 13, 2017 at 17:16

90 Answers 90

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\$
2
\$\begingroup\$

Pyke, 1 byte

q

Try it here!

This is the only 1 byte solution that works in Pyke, all the others have collisions.

\$\endgroup\$
2
\$\begingroup\$

Javascript (ES6), 10 Bytes

x=>y=>!x+y

Put f= before the code and run like f(x)(y).

This will output y plus either 0 or 1 depending on whether x is truthy or falsy, respectively.

Thanks to TomDevs for the bytes saved

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can save 1 byte by currying and the output doesn't have to be 0 or 1, so you can save a further 4 bytes: x=>y=>!x+y, at is ran by (x=>y=>!x+y)(1)(2) \$\endgroup\$
    – Tom
    Feb 13, 2017 at 11:28
  • \$\begingroup\$ @TomDevs Oh yea I totally forgot about currying and thanks for the output tip as well. \$\endgroup\$
    – user64039
    Feb 13, 2017 at 11:42
2
\$\begingroup\$

QBIC, 8 bytes

a bit uninteresting:

::?a+b+1

This just takes A and B from the cmd-line, adds them plus 1.

\$\endgroup\$
2
\$\begingroup\$

CJam, four 3 bytes

q~=

Try it online!

Explanation

q~   e# Read the input and eval it (pushes the numbers on the stack).
  =  e# Check equality.
\$\endgroup\$
2
\$\begingroup\$

Javascript, 12 bytes

(a,b)=>a+b+1

Accepts 2 numbers inside a closed lambda and return their sum plus 1

Example:

((a,b)=>a+b+1)(1, 2)
// 4
\$\endgroup\$
5
  • \$\begingroup\$ You can save two bytes by removing the outer pair of parentheses :-) \$\endgroup\$ Feb 13, 2017 at 15:01
  • \$\begingroup\$ I think that will break the IIFE \$\endgroup\$
    – svarog
    Feb 13, 2017 at 15:03
  • 2
    \$\begingroup\$ We allow any expression that returns a reusable function as a valid entry. Here, (a,b)=>a+b+1 is a perfectly valid function expression; you can assign it to a function with f= and then call it like f(1, 2) as many times as you want. \$\endgroup\$ Feb 13, 2017 at 16:21
  • 1
    \$\begingroup\$ currying, i.e. a=>b=>a+b+1 is shorter. you then call it with f(1)(2) \$\endgroup\$
    – FlipTack
    Feb 13, 2017 at 19:55
  • \$\begingroup\$ Does not work for inputs 1e53 and 0. \$\endgroup\$ Aug 20, 2020 at 14:46
2
\$\begingroup\$

O, 4 bytes

jj=o

The same as other implementations. Get the numbers, test the equality and output. Try it here

j    Number input
 j   Number input
  =  Equal to
   o Print object
\$\endgroup\$
2
\$\begingroup\$

Forth, 5 bytes

I didn't see Forth on the list, this is the A+B+1 solution.

+ 1 +

It adds the top 2 numbers on the stack, then adds one to it.

The answer will be the top value on the stack.

Example:

=> 4, 5
+ 1 +
=> 10
\$\endgroup\$
1
  • \$\begingroup\$ This can be + 1+, since there is a separate word for 1+. But according to other answers, you could just use =. Also, I'm pretty sure Forth needs to use . for output unless you define a new word. This is as close to a snippet as you can get. \$\endgroup\$
    – mbomb007
    Mar 8, 2017 at 14:38
2
\$\begingroup\$

Clojure, 11 bytes

#(+ % %2 1)

Unoriginal. Just adds 1 to the sum of % and %2 (the arguments to the function).

\$\endgroup\$
2
\$\begingroup\$

Beam, 4 bytes

This seems a bit cheaty, but it reads from the input (characters only) and outputs the ASCII values for each.

>r:!

Try it online!

>     # redirect right
 r    # read input character
  :   # output numeric value of beam
   !  # if beam <> 0 reflect back

The Halt command is required on here as the beam just disappears into the aether.

\$\endgroup\$
2
\$\begingroup\$

Cheddar, 11 bytes

(a,b)->a-~b

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Hexagony, 11 bytes

?}?@!<."+)/

Prints X + Y + 1

Try it online!


The expanded Hex:

  ? } ?
 @ ! < .
" + ) / .
 . . . .
  . . .
  1. read into the initial memory edge.
  2. go forward to the right and read into that one.
  3. wrap around to the middle row.
  4. go backward to the left and sum the other edges into this one.
  5. increment.
  6. redirect NW then W to save a byte.
  7. print and exit.
\$\endgroup\$
2
\$\begingroup\$

SQLite, 23 22 bytes

Saved 1 byte thanks to MickyT

select 1+sum(v)from i;

By default, SQL variants can take input from a named table. In this case it takes two numbers from the column v in the table i, sums them, and adds 1.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save a byte switching the order of the addition and removing the space before the from. select 1+sum(v)from i; \$\endgroup\$
    – MickyT
    Feb 14, 2017 at 20:55
2
\$\begingroup\$

C (gcc), 13 bytes

n(a,b){a=&b;}

returns b's address in memory, though it's slighty cheaty since the user is able to predict the address of b.

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Very nice! I was trying to get less than 14 and couldn't! \$\endgroup\$ Feb 26, 2017 at 18:57
  • \$\begingroup\$ why not n(a){a=&a;} \$\endgroup\$
    – l4m2
    Dec 15, 2017 at 18:50
  • \$\begingroup\$ @l4m2 The question requires 2 inputs \$\endgroup\$
    – Ahemone
    Dec 17, 2017 at 9:37
  • \$\begingroup\$ n(a){a=&a;} main(a,b){return n(1,2);} works fine \$\endgroup\$
    – l4m2
    Dec 17, 2017 at 10:05
  • \$\begingroup\$ Do you know why that works? \$\endgroup\$
    – Ahemone
    Dec 17, 2017 at 10:25
2
\$\begingroup\$

PHP, 22 bytes

eval("echo $argn+1;");

A port of ConnorLSW´s answer. Run with echo "<a>+<b>" | php -R '<code>'.

\$\endgroup\$
2
+50
\$\begingroup\$

Milky Way 2 bytes

b!

Checks equality.

http://meta.codegolf.stackexchange.com/a/8493/63187 allows me to assume the inputs are pushed to stack.

Thank you Zach Gates! I out golfed him in his own language :P

\$\endgroup\$
3
  • \$\begingroup\$ You still need to output your result (use ! or ¡). Best alternative I've come up with so far is l+¡. \$\endgroup\$
    – Zach Gates
    Mar 2, 2017 at 15:04
  • \$\begingroup\$ @ZachGates OK thanks! Good luck \$\endgroup\$
    – user63187
    Mar 2, 2017 at 15:13
  • \$\begingroup\$ @ZachGates one I had was +R+! Then found the other way \$\endgroup\$
    – user63187
    Mar 2, 2017 at 16:13
2
\$\begingroup\$

Python, 25 17 bytes

Takes an array of numbers n, and returns their sum + 1:

lambda n:sum(n,1) 

I just translated https://codegolf.stackexchange.com/a/111845/66068 into python.

\$\endgroup\$
1
  • \$\begingroup\$ Anonymous functions are allowed here so you can remove the f= to save 2 bytes. Also replace nums with n to save 6 bytes. lambda n:sum(n,1) for 17 bytes. Also your original is 25 bytes. \$\endgroup\$ Apr 1, 2017 at 11:59
2
\$\begingroup\$

Keg, 5 bytes

Input two natural numbers separated with spaces.

?(+).

Sums the whole stack. The answer is guaranteed to be invalid, since what it sums is the charcodes of the numbers.

TIO

\$\endgroup\$
2
\$\begingroup\$

naz, 56 bytes

6a8m2x1v1x1f1r3x1v2e0m1o0x1x2f2r3x1v3e0m1o0x1x3f1a1o0x1f

Takes two natural numbers as input, separated by a space.

Explanation (with 0x commands removed)

6a8m2x1v         # Set variable 1 equal to 48 ("0")
1x1f1r3x1v2e0m1o # Function 1
                 # Read the first byte of input, removing it from the input string
                 # Jump to function 2 if it equals variable 1
                 # Otherwise, output 0
1x2f2r3x1v3e0m1o # Function 2
                 # Read the second byte that's still in the input string
                 # Jump to function 3 if it equals variable 1
                 # Otherwise, output 0
1x3f1a1o         # Function 3
                 # Output 1
1f               # Call function 1
\$\endgroup\$
2
\$\begingroup\$

Flurry, 12 bytes

[]{<{}{}>}{}

Run example

$ printf "0 0" | ./flurry -nib -c "[]{<{}{}>}{}"
73728
$ printf "0 1" | ./flurry -nib -c "[]{<{}{}>}{}"
75264
$ printf "0 11" | ./flurry -nib -c "[]{<{}{}>}{}"
3687936

Takes two numbers separated by single space from stdin in binary mode, and prints the return value as an integer.

The binary mode is the trick here. What the program does is to implicitly push all codepoints of stdin to the stack, and print the product of the entire stack.

Code explanation

// Pop (stack height + 1) times and compute the product;
// popping from empty stack gives I = 1
//     []     {<{}{}>}  {}
main = height times-pop pop

//          {   <{}  {}  >}
times-pop = \x.  x ∘ pop

Proof that this does not give a+b for any a b

Base case: The input of "0 0" gives 48 × 32 × 48 = 73728.

Inductive case:

  • The codepoint of any printable ASCII is 32 or above, so adding a digit (which amplifies a+b by at most 10) amplifies the output by 32 or above.
  • Adding 1 to an existing digit adds the product of all the codepoints except itself to the output, which is much greater than the digit value of the modified digit.

Therefore, the output grows much faster than the value of a+b, and they can never be equal.


For comparison, here is the program that computes a+b+1:

Flurry, 18 bytes

{}(<><<>()>)[{}{}]
$ ./flurry -nin -c "{}(<><<>()>)[{}{}]" 12 13
26
$ ./flurry -nin -c "{}(<><<>()>)[{}{}]" 0 0
1
$ ./flurry -nin -c "{}(<><<>()>)[{}{}]" 0 10
11
// initial stack = a b
//     {}  (<><<>()>)  [{}   {} ]
main = pop (push succ) (pop  pop)
//     b                succ a
     = b succ (succ a)
     = b + succ a = a + b + 1
\$\endgroup\$
2
\$\begingroup\$

Haskell, 8 bytes

(+).succ

Try it online!

I'm certain this could be shorter but I haven't found a solution yet.

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

Lua, 25 bytes

x={...}print(x[1]+x[2]+1)

Try it online!

Just a trivial A+B+1 solution.

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

Chaincode, 2 bytes

a+

Explanation:

a   #Adds two input values
 +  #Increment
\$\endgroup\$
1
\$\begingroup\$

Labyrinth, 6 bytes

??+)!@

Try it online!

Takes two number inputs, prints their sum + 1.

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

Scala, 26 Bytes

var n=(a:Int,b:Int)=>a+b+1
\$\endgroup\$
1
\$\begingroup\$

Clack, 3 bytes

1++

Since clack has very few instructions at the moment, the shortest method is to just push 1, the two numbers, and add them.

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

Powershell, 15 Bytes

("$args"|iex)+1

input is in any standard mathematical format, 1+1,2+2,5123-123213,200*99,1234/555 etc.

Just takes input, invokes (calculates) it, then adds one at the end.

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

C, 18 bytes

#define f(a,b)a==b

try it online

\$\endgroup\$
1
  • 1
    \$\begingroup\$ #define f(a)a== calling currying lol \$\endgroup\$
    – l4m2
    Dec 17, 2017 at 10:09

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.