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\$ – Kritixi Lithos 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\$ – Post Rock Garf Hunter Feb 13 '17 at 17:16

76 Answers 76

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\$
  • 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 '17 at 11:28
  • \$\begingroup\$ @TomDevs Oh yea I totally forgot about currying and thanks for the output tip as well. \$\endgroup\$ – user64039 Feb 13 '17 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\$
  • \$\begingroup\$ You can save two bytes by removing the outer pair of parentheses :-) \$\endgroup\$ – ETHproductions Feb 13 '17 at 15:01
  • \$\begingroup\$ I think that will break the IIFE \$\endgroup\$ – svarog Feb 13 '17 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\$ – ETHproductions Feb 13 '17 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 '17 at 19:55
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\$
  • \$\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 '17 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\$
  • \$\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 '17 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\$
  • \$\begingroup\$ Very nice! I was trying to get less than 14 and couldn't! \$\endgroup\$ – Albert Renshaw Feb 26 '17 at 18:57
  • \$\begingroup\$ why not n(a){a=&a;} \$\endgroup\$ – l4m2 Dec 15 '17 at 18:50
  • \$\begingroup\$ @l4m2 The question requires 2 inputs \$\endgroup\$ – Ahemone Dec 17 '17 at 9:37
  • \$\begingroup\$ n(a){a=&a;} main(a,b){return n(1,2);} works fine \$\endgroup\$ – l4m2 Dec 17 '17 at 10:05
  • \$\begingroup\$ Do you know why that works? \$\endgroup\$ – Ahemone Dec 17 '17 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\$
  • \$\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 '17 at 15:04
  • \$\begingroup\$ @ZachGates OK thanks! Good luck \$\endgroup\$ – Christopher Mar 2 '17 at 15:13
  • \$\begingroup\$ @ZachGates one I had was +R+! Then found the other way \$\endgroup\$ – Christopher Mar 2 '17 at 16:13
2
\$\begingroup\$

///, 10 bytes + input

/0/1//+//[input A]+[input B]

Obviously [input A] and [input B] are supposed to be replaced with the appropriate inputs

Try it online!

Changes 0's to 1's, then concatenates the strings.

This works because:

  • For A,B>0, join(A,B)>A+B
  • join(A,B)>join(C,D) if A>C or B>D
  • The function f(x)='change 0's to 1's in the digits of x' is always at least x

So we have join(f(A),f(B))>f(A)+f(B)>=A+B.

And if one or both of the inputs are 0, we have join(1,B)>1+B>0+B, join(A,1)>A+1>A+0 and join(1,1)=11>0+0.

Note 1: This type of input has been approved by the community
Note 2: This output doesn't give leading 0's either
Note 3: Less bytes than Python, PHP, C#, C, Powershell and more!

\$\endgroup\$
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\$
  • \$\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\$ – caird coinheringaahing Apr 1 '17 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\$
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\$
  • \$\begingroup\$ #define f(a)a== calling currying lol \$\endgroup\$ – l4m2 Dec 17 '17 at 10:09
1
\$\begingroup\$

<><, 3 bytes

Also see this post, but <>< needs the explicit print and termination so we can't go under 3 bytes.

=n;

But this is a pretty boring solution. There are two 5 byte solutions

1nnn;

and

1++n;

that are only slightly more interesting. For input a and b, the first one works by printing 1ba and the second by printing a+b+1.

Here is a TIO test bench where bytes 2-5 should be replaced by the first 4 bytes of a solution.

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

Java - 44 bytes

int n(int a,int b){return Math.abs(a+(-b));}

Returns an absolute value of a + (-b)

Ungolfed version:

int n(int a, int b) {
    return Math.abs(a + (-b));
}
\$\endgroup\$
  • \$\begingroup\$ Wouldn't it fail for 0 and 0? \$\endgroup\$ – MatthewRock Mar 1 '17 at 23:19
  • \$\begingroup\$ @MatthewRock didnt check that. I willfix it tomorrow \$\endgroup\$ – cookie Mar 1 '17 at 23:23
  • \$\begingroup\$ Isn't a+(-b) equivalent to a-b? \$\endgroup\$ – Zacharý Sep 16 '17 at 13:41

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.