45
\$\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).

asked Feb 13, 2017 at 1:35
\$\endgroup\$
10
  • 1
    \$\begingroup\$ Maybe we take the numbers as strings and output as a string? \$\endgroup\$ Commented Feb 13, 2017 at 3:01
  • 1
    \$\begingroup\$ Can the output have leading zeroes? \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Feb 13, 2017 at 17:16

94 Answers 94

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));
}
answered Feb 26, 2017 at 12:03
\$\endgroup\$
3
  • \$\begingroup\$ Wouldn't it fail for 0 and 0? \$\endgroup\$ Commented Mar 1, 2017 at 23:19
  • \$\begingroup\$ @MatthewRock didnt check that. I willfix it tomorrow \$\endgroup\$ Commented Mar 1, 2017 at 23:23
  • \$\begingroup\$ Isn't a+(-b) equivalent to a-b? \$\endgroup\$ Commented Sep 16, 2017 at 13:41
1
\$\begingroup\$

ForceLang, 33 bytes

def a io.readnum()
io.write a+a+1
answered Feb 26, 2017 at 12:16
\$\endgroup\$
2
  • \$\begingroup\$ Does this take two numbers as input? \$\endgroup\$ Commented Feb 26, 2017 at 12:55
  • \$\begingroup\$ @muddyfish Yes. \$\endgroup\$ Commented Feb 26, 2017 at 12:59
1
\$\begingroup\$

Cardinal, 7 bytes

%:+~:*.

Outputs sum + 1

Try it online!

answered Feb 28, 2017 at 14:56
\$\endgroup\$
1
\$\begingroup\$

Melang (Non competing) 13 bytes

\n\n+\n+\n\ni

I wrote Melang for this challenge and decided that it works so might as well bring it here.

Adds the numbers inputted together and adds one. For more on the language and to try it out visit here.

answered Mar 7, 2017 at 16:33
\$\endgroup\$
7
  • \$\begingroup\$ Wait, those aren't literal newlines? \$\endgroup\$ Commented Apr 10, 2017 at 9:30
  • \$\begingroup\$ @ascii nope! Literal newlines error my implementation due to the website restrictions \$\endgroup\$ Commented Apr 11, 2017 at 1:33
  • \$\begingroup\$ How does it error in your implementation? \$\endgroup\$ Commented Apr 11, 2017 at 1:35
  • \$\begingroup\$ @ascii the way string inputs are taken in the website I used to write it breaks it \$\endgroup\$ Commented Apr 11, 2017 at 1:40
  • \$\begingroup\$ well that's a JS implementation limitation not a melang implementation limitation, you do know you can make it a Stack Snippet and it would work right? \$\endgroup\$ Commented Apr 11, 2017 at 1:41
1
\$\begingroup\$

Haskell, (削除) 11 (削除ここまで) 9 bytes

(succ.).(+)

Adds together and then adds 1

EDIT: Of course, if you want to go with the boring answer:

a#b=a+b+1

is 9 bytes.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Non pointfree a#b=1+a+b is two bytes shorter. \$\endgroup\$ Commented Apr 1, 2017 at 9:21
1
\$\begingroup\$

c++, 28 bytes

[](int a,int b){return!a+b;}

Try it here

answered Apr 5, 2017 at 13:41
\$\endgroup\$
1
\$\begingroup\$

Japt, 3 bytes

NxÄ

Try it online


Explanation

N :The array of all inputs
x :Sum the array
Ä :Add one to each element
answered May 23, 2017 at 10:08
\$\endgroup\$
1
\$\begingroup\$

Aceto, 9 bytes

ir+1
ri+p

Super straightforward. Prints the sum of the two inputs and 1. If I can assume for the inputs to be on the stack already, there's a 5-byte solution:

1+
+p
answered May 23, 2017 at 10:12
\$\endgroup\$
1
\$\begingroup\$

MarioLANG, 35 bytes

;
)
;
>[!(+:
"=#===
) -
+ (
! <
#="

Reads two inputs and puts them in differents cells (variables). Increments one and decrements the other until the second number is zero. Then adds one calculate the wrong answer.

answered May 28, 2017 at 7:39
\$\endgroup\$
1
\$\begingroup\$

Underload, 23 bytes

((::)~*)~(~)~*a*^*(**)*

Takes input in the form of Church numerals on the stack and outputs to the stack.

Try it online!

answered Jun 19, 2017 at 15:58
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 13 Bytes

Anonymous VBE immediate window function that takes input from range [A1:B1] (... or really any range that is a subset of [1:1], I suppose) and outputs their sum +7

?[Sum(1:1)+7]
answered Jul 9, 2017 at 15:11
\$\endgroup\$
1
\$\begingroup\$

Windows batch, (削除) 24 23 (削除ここまで) 17 bytes

@cmd/cset/a%1+%21

Test cases:

Tested on Win10 64-bit

Foo\Bar\Baz>NotCalc.bat 1 55
552

Note: batch files are limited to 32-bit signed integer!

answered Feb 27, 2017 at 10:25
\$\endgroup\$
6
  • 1
    \$\begingroup\$ isn't 0+0 with a trailing 0 00, which is 0? \$\endgroup\$ Commented Mar 31, 2017 at 20:58
  • \$\begingroup\$ Oh yeah. I forgot that \$\endgroup\$ Commented Apr 1, 2017 at 1:36
  • \$\begingroup\$ I tested it for you (also on Win7 64-bit). 1+55 returns 552. I don't understand the program well enough to explain why it returned that and not 561, but I tried some other inputs and got more weird results like 5+9=96. I'm not sure why that is, but my limited testing didn't find anything that outputs a correct sum... \$\endgroup\$ Commented Apr 1, 2017 at 4:03
  • \$\begingroup\$ It adds a trailing one to 55 so it becomes 551, 1+551=552 and 9 became 91, 5+91=96 \$\endgroup\$ Commented Apr 1, 2017 at 5:18
  • 1
    \$\begingroup\$ Oh, I see. I thought it was adding a 1 to the result. \$\endgroup\$ Commented Apr 1, 2017 at 5:21
1
\$\begingroup\$

Perl 5, 8 bytes

7 bytes of code + 1 flag (-p)

$_+=!<>

Try it online!

answered Sep 16, 2017 at 1:28
\$\endgroup\$
1
  • \$\begingroup\$ Pretty sure perl gets -p free. \$\endgroup\$ Commented Nov 16, 2017 at 3:32
1
\$\begingroup\$

Aceto, 6 bytes

rrJiIp
r reads input and puts on stack (x2)
J concatenates top 2 values
i makes it an integer
I increments it
p prints it

Try it online!

answered Nov 18, 2017 at 16:22
\$\endgroup\$
1
\$\begingroup\$

Implicit, 2 bytes

+.

Try it online!

+. implicit input of two integers
+ add them
 . and increment

= also works for 1 byte, but I added that to the existing list of =s.

answered Nov 16, 2017 at 3:31
\$\endgroup\$
1
\$\begingroup\$

x86 opcode, 2 bytes

INC EDX
RET

input EAX and EDX, output EDX:EAX If D':A=D+A then D has to be zero, making the equation still not correct

x86 opcode __cdecl, 3 Bytes

MOV DL, 3
RET
answered Dec 16, 2017 at 8:28
\$\endgroup\$
1
\$\begingroup\$

MAWP, 4 bytes

@MM:

adds the numbers to the 1 in stack.

Try it!

answered Aug 20, 2020 at 4:06
\$\endgroup\$
7
  • 3
    \$\begingroup\$ Fails at 0 0, giving 0 as output. \$\endgroup\$ Commented Aug 20, 2020 at 4:08
  • \$\begingroup\$ Invalid submissions need to be deleted, sorry :P. \$\endgroup\$ Commented Aug 20, 2020 at 4:09
  • \$\begingroup\$ For our purposes, 0 is natural (although not all mathematicians agree). does this note not apply? \$\endgroup\$ Commented Aug 20, 2020 at 4:21
  • \$\begingroup\$ Of course it apply, so your program is invaild. \$\endgroup\$ Commented Aug 20, 2020 at 4:29
  • 3
    \$\begingroup\$ The problem isn't that 0 is natural. The problem is that for an input of 0 0, your code outputs 0, which is the sum of the two inputs. \$\endgroup\$ Commented Aug 20, 2020 at 4:30
1
\$\begingroup\$

Google Sheets, 9

=--(A1=A2
  • If A1=A2, the sum is even, but the output is always 1, which is odd.
  • If A1<>A2, then the sum is positive, but the output is always 0, which is not positive.

Why didn't I use =A1+A2+1? Because if I input a sufficiently large number and 0, the +1 does nothing.

answered Aug 20, 2020 at 14:40
\$\endgroup\$
1
\$\begingroup\$

HBL, 2 bytes

+.,1

Try it at HBL Online!

Explanation

+ Add
 . the first argument
 , the second argument
 1 and one
answered Nov 25, 2021 at 20:02
\$\endgroup\$
1
\$\begingroup\$

Braingolf, 3 bytes

g1+

Appends the inputs to eachother (ie 17, 4 becomes 174), then increments by 1.

Notable testcases:

[0, 0] becomes 00, becomes 01
[0, 1] becomes 01, becomes 02
[1, 0] becomes 10, becomes 11
[1, 1] becomes 11, becomes 12
emanresu A
46.2k5 gold badges111 silver badges257 bronze badges
answered May 23, 2017 at 10:28
\$\endgroup\$
1
  • \$\begingroup\$ 00 is equivalent to 0+0 = 0 as noted in the comments for Windows Batch (among others) \$\endgroup\$ Commented May 28, 2017 at 4:01
1
\$\begingroup\$
answered Jun 8, 2023 at 16:55
\$\endgroup\$
1
\$\begingroup\$

Vyxal HṪ, 2 bytes

しろいしかく

I had to post this as it is quite obfuscated.

Try it Online!

Explanation

しろいしかく∑­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌­
しろいしかく # ‎⁡Get input treated as a list separated by newlines.
 ∑ # ‎⁢Sum the list together.
💎 Created with the help of Luminespire at https://vyxal.github.io/Luminespire

With the explanation above, it seems like that this returns the correct sum. But if you think this, then you haven’t paid attention to the flags.

HṪ­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌­
H # ‎⁡Before execution, push 100 to the stack.
 Ṫ # ‎⁢After execution, take the sum of the whole stack and print it.
💎 Created with the help of Luminespire at https://vyxal.github.io/Luminespire

Now that you have the full picture, you realise that it adds 100 to the result.

answered Jul 6, 2023 at 15:53
\$\endgroup\$
1
\$\begingroup\$

Acc!!, 31 bytes

Count i while N-10 {
Write 57
}

Takes the two numbers space-separated. Try it online!

Explanation

Count i while N-10 { # While input character is not a newline:
 Write 57 # Output 9
}

To prove that the output is always valid, consider the following numbers:

  • A + B = the sum of the input numbers
  • AB = the concatenation of the input numbers (equal to A * 10 ^ (len B) + B)
  • N = a number of the same length as AB but composed of all 9's
  • N+ = N with an extra 9 concatenated (equal to N * 10 + 9)

The output of this program is N+ (the extra 9 comes from the space between the input numbers). Looking at the formulas, we have A + B <= AB, AB <= N, and N < N+; therefore, this program's output is always greater than (and therefore not equal to) A + B.

answered Jul 6, 2023 at 18:55
\$\endgroup\$
1
  • \$\begingroup\$ (you can omit the trailing } and newline) \$\endgroup\$ Commented Aug 7 at 23:48
1
\$\begingroup\$

brainfuck, 18 bytes

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

Try it online!

Output is always lot larger than input

answered Jul 7, 2023 at 9:14
\$\endgroup\$
1
\$\begingroup\$

Zsh, 8 bytes

<<<91ドル2ドル

Try it online!

Concatenates 9 A B which is always larger than A + B

answered Jul 10, 2023 at 10:30
\$\endgroup\$
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.

noodle person
12.6k1 gold badge31 silver badges90 bronze badges
answered Feb 26, 2017 at 11:08
\$\endgroup\$
1
\$\begingroup\$

Setanta, (削除) 29 (削除ここまで) 28 bytes

Thanks to General Grievance for −1 byte

gniomh(a,b){toradh a==b|0&1}

Returns 1 if the arguments are equal; 0 otherwise.

try-setanta.ie link

answered Aug 7 at 18:34
\$\endgroup\$
2
  • \$\begingroup\$ Holy smokes, an Irish-based programming language? \$\endgroup\$ Commented Aug 7 at 18:35
  • \$\begingroup\$ Cad faoi gniomh(a,b){toradh a==b|0&1}? 28 bytes. 1 if equal (sum must be even), 0 if not (sum must be >0). \$\endgroup\$ Commented Aug 7 at 19:24
1
\$\begingroup\$

Bespoke, 49 bytes

using C
added a specific pair
segfault
o,stupid C

Outputs !(A+B).

answered Aug 8 at 4:30
\$\endgroup\$
0
\$\begingroup\$

GolfScript, 3 bytes

Input is to be taken as two separate lines. In GolfScript, a preceding 0 is allowed for numbers.

n/0

Try it online!

Explanation

# Two inputs taken with newlines
n/ # Split the input (separated by newlines) with newlines
 0 # Add a 0 to prevent a preceding 0
 # Implicit concatenation

GolfScript, 3 bytes

~+)

Try it online!

Explanation

~ # Evaluate the input
 + # Add the two numbers
 ) # Increment the sum
answered Jan 26, 2020 at 8:56
\$\endgroup\$
0
\$\begingroup\$

1+, 6 bytes

..+1+:

As usual. a + b + 1.

answered Aug 20, 2020 at 4:26
\$\endgroup\$
2
  • \$\begingroup\$ Just FYI, a few of your 1+ answers have been popping up in the low-quality posts queue recently (autoflagged). I haven't noticed any problems with the code in any of these posts, but I do think your answers would benefit from a bit more explanation. \$\endgroup\$ Commented Aug 20, 2020 at 5:21
  • \$\begingroup\$ @Dingus Oh, ok. Although this answer (and a couple others) is really naive and I don't see how to add an explanation... \$\endgroup\$ Commented Aug 20, 2020 at 6:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.