40
\$\begingroup\$

Problem:

Your goal is to add two input numbers without using any of the following math operators: +,-,*,/.

Additionally, you can't use any built-in functions that are designed to replace those math operators.

Scoring:

Smallest code (in number of bytes) wins.

Update

Most of the programs i've seen either concatenate two arrays containing their numbers, or make first number of a character, append second number characters, then count them all.

Shortest array counter: APL with 8 chars, by Tobia

Shortest array concatenation: Golfscript with 4 chars, by Doorknob

Shortest logarithmic solution: TI-89 Basic with 19 chars, by Quincunx

Integration solution: Mathematica with 45 chars, by Michael Stern

Coolest, in my opinion: bitwise operators in javascript, by dave

\$\endgroup\$
13
  • 1
    \$\begingroup\$ Will it have floats? \$\endgroup\$ Commented Feb 16, 2014 at 1:06
  • 7
    \$\begingroup\$ Will it have negative numbers? (Currently, all the answers assume that the numbers will be positive, so you probably shouldn't change that) \$\endgroup\$
    – Doorknob
    Commented Feb 16, 2014 at 3:48
  • 4
    \$\begingroup\$ What about the mathematical solutions? You forgot to list those! This integrates, and this plays with logarithms \$\endgroup\$
    – Justin
    Commented Feb 17, 2014 at 0:54
  • 3
    \$\begingroup\$ Why did you accept one of the longer solutions? Is it because it accepts negative numbers while the shortest solutions (this and this) don't? If so, my answer supports negative numbers (it also supports floating point) and is shorter than this one. You tagged this question as code-golf, thus you are obliged to accept the shortest solution. \$\endgroup\$
    – Justin
    Commented Feb 20, 2014 at 21:01
  • 3
    \$\begingroup\$ Define "number". Any integer? Non-negative integers? Do they have to be base-10? \$\endgroup\$ Commented Jan 20, 2017 at 13:44

89 Answers 89

1 2
3
0
\$\begingroup\$

Go [43 bytes]

len(append(make([]int,a),make([]int,b)...))
\$\endgroup\$
0
\$\begingroup\$

Perl, 38 chars

for(0..1){push @a,0for(1..<>)}print~~@a
\$\endgroup\$
0
\$\begingroup\$

Ruby 49 47

a=gets;b="";gets.to_i.times{b+=".succ"};eval(a+b)

a=gets;b=gets;a.to_i.times{b+=".succ"};eval(b)
\$\endgroup\$
0
\$\begingroup\$

Batch - 71 bytes

For every iteration of a for loop, which will count to each inputted number, this outputs a . to a file named f. It then uses find to count the number of appearances of ..

@(for %%a in (%1 %2)do @for /l %%b in (1,1,%%a)do.)>f 2>e&@find/c"." f

-

h:\MyDocuments\uprof>test.bat 2 4

---------- F: 6

Old Method - 157 bytes

@setLocal enableDelayedExpansion&for /L %%a in (1,1,%1)do @set o=!o!.
@for /L %%b in (1,1,%2)do @set o=!o!.
@set/p"=%o%"<nul>f&for %%c in (f)do @echo %%~zc

This will take two values as input, count up to them, creating a variable that is the length of the two values added, then output that to a file (using set /p trick instead of echo to avoid a carriage return, which would add an extra two bytes), then gets the size of the file in bytes, which will be the sum of the two input values.

H:\uprof>test.bat 2 3
5

H:\uprof>test.bat 25 112
137
\$\endgroup\$
0
\$\begingroup\$

Python (36)

I think I'll switch to this for daily use , it's very efficient.

c=range(a);c.extend(range(b));len(c)
\$\endgroup\$
0
\$\begingroup\$

Python, 118

This uses a binary approach. I didn't really try to golf it as much as I tried to make it interesting; I've already built binary addition in Minecraft in an 8x3x5 space per bit (!!!) and wanted to compare.

r,c="",0
if b>a:a,b=b,a
while a:
 a,b,e,d=a>>1,b>>1,a%2,b%2
 r=str(e^d^c)+r;c=(c&d)|(c&e)|(d&e)
r=int(str(c)+r,2)

Thanks to @Sp3000 for shaving off about 50 chars and getting it to actually work in the chat ;D

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

Python 3, 27 bytes

len("".join(["."*x,"."*y]))

where x is input 1 and y is input 2

(Okay, okay fine it's really 49 bytes including "true code":

len("".join(["."*int(input()),"."*int(input())]))

Note that * is for string duplication, although that might be a "standard operator"

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

C, 14 bytes

while(x--)y++;

Avoids +, -, * and /.

Note that ++ and -- compile to machine code for INCR and DECR, so there is no addition or subtraction.

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

Dart - 30

s(a,b)=>a>0?s((a&b)<<1,a^b):b;

After writing this, I realized it was equivalent to the JavaScript solution already posted above - and because Dart has bignums, not just int32, it doesn't work for negative numbers. So, not a big win :(

Termination can be proven by seeing that the number of bits in a and b together reduces for each iteration until a becomes zero.

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

Microscript, 9 bytes

A variant of the array concatenation solution (although this one uses a stack.)

ics]ics]#

Technically not a valid competing entry, however; as the language is too new.

Requires that both inputs be nonnegative.


A seven-byte version using a new command added shortly after I originally posted this:

i$si$s#

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

X86 (2)

rep movb two bytes f3 A4,

adds ecx to esi (and to edi) result in esi but has side-effects if ds:esi != es:edi

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

Python 2, Python 3, 41 bytes

i,j=input(),input()
while j>0:i=-~i;j=~-j

If using the - number operator is allowed.


Python 2, Python 3, 40 bytes

i,j=input(),input()
while j>0:i=-~i;j-=1

If using the - number and literal -= number operators is allowed.


Works on positive integers. Takes a and b and prints a+b.

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

Java, 85 bytes

import java.math.*;class a{BigInteger A(BigInteger b,BigInteger B){return b.add(B);}}

BigInteger isn't located in java.lang, java.util, java.io (is it even possible for a numeric type to be related to I/O?) or their subpackages, so its add method doesn't count as a language feature. Problem?

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

JAVA, 79 bytes

int Add(int x, int y){while (y != 0){int carry=x&y;x=x^y;y=carry<<1;}return x;}
\$\endgroup\$
0
\$\begingroup\$

PHP, 91 78 51 bytes

bit logic and shifting ... what else?

function p($x,$y){return$y?p($x^$y,($x&$y)<<1):$x;}

also works for negative integers

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

Haskell, 25 bytes

n+m=length$[1..m]++[1..n]

We can no longer use the old +? No problem, we can just define a new one. This approach doesn't work with negative numbers, I have yet to figure out something else for them.

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

R, 80 bytes

Went for a recursive function doing bit operations.

f=function(a,b){d=bitwXor(a,b);e=bitwShiftL(bitwAnd(a,b),1);ifelse(!e,d,f(d,e))}

Ungolfed:

f=function(a,b){
    d=bitwXor(a,b)            # xors a and b.(tells us bits that are 1 in end)
    f=bitwAnd(a,b)            # tells us which bits need to carry over 1
    e=bitwShiftL(e,1)         # carries those bits over
    if (e==0) d else f(d,e)   # if carried nothing, return xored bits. 
}                             # otherwise, recursively call fn on xored and
                              # carried bits.

It would have been shorter -- but R really does not intend for bit operations.

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

Turing Machine Code, 1184 bytes

A golfed version of a machine I posted to my googology wiki user blog a while ago. Supports signed ints. Try it online!

0 _ * l B
0 - * r AAA
0 * * r *
B 0 9 l *
B 9 8 r C
B 8 7 r C
B 7 6 r C
B 6 5 r C
B 5 4 r C
B 4 3 r C
B 3 2 r C
B 2 1 r C
B 1 0 r C
B _ * r AA
B * * * AA
C _ * r D
C * * r *
D _ * l E
D - * r 8
D * * r *
E 0 1 l F
E 1 2 l F
E 2 3 l F
E 3 4 l F
E 4 5 l F
E 5 6 l F
E 6 7 l F
E 7 8 l F
E 8 9 l F
E 9 0 l *
E _ * r S1
E M * r S1
F _ * l B
F * * l *
AA _ * r BB
AA * _ r *
BB M - * halt
BB * * * halt
S1 0 1 r S0
S0 0 0 r S0
S0 _ 0 * F 
AAA 0 9 l *
AAA 9 8 r BBB
AAA 8 7 r BBB
AAA 7 6 r BBB
AAA 6 5 r BBB
AAA 5 4 r BBB
AAA 4 3 r BBB
AAA 3 2 r BBB
AAA 2 1 r BBB
AAA 1 0 r BBB
AAA _ * r BB
AAA * * * BB
BBB _ * r CCC
BBB * * r *
CCC _ * l DDD
CCC - * * ZZZ
CCC * * r *
DDD 9 8 l EEE
DDD 8 7 l EEE
DDD 7 6 l EEE
DDD 6 5 l EEE
DDD 5 4 l EEE
DDD 4 3 l EEE
DDD 3 2 l EEE
DDD 2 1 l EEE
DDD 1 0 l EEE
DDD 0 9 l *
DDD _ * r FFF
EEE _ * l *
EEE * * * AAA
FFF _ * * GGG
FFF * _ r *
GGG _ * l *
GGG * * * HHH
HHH 0 1 * halt
HHH 1 2 * halt
HHH 2 3 * halt
HHH 3 4 * halt
HHH 4 5 * halt
HHH 5 6 * halt
HHH 6 7 * halt
HHH 7 8 * halt
HHH 8 9 * halt
HHH 9 0 l *
8 _ * l 9
8 * * r * 
9 9 8 l F
9 8 7 l F
9 7 6 l F
9 6 5 l F
9 5 4 l F
9 4 3 l F
9 3 2 l F
9 2 1 l F
9 1 0 l F
9 0 9 l *
9 * * * FFF
ZZZ * M r E
\$\endgroup\$
1
  • \$\begingroup\$ can't you use shorter state names :| \$\endgroup\$
    – ASCII-only
    Commented Apr 12, 2019 at 7:18
0
\$\begingroup\$

Julia, 29 bytes

a$b=next(countfrom(a,b),a)[2]

Julia has iterators that can count up by an arbitrary amount at a time. It's usually used to create arithmetic sequences, but it can be definitely pressed into service as a ersatz adder.

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

Java 8, 42 bytes

(x,y)->{for(int c;y!=0;c=x&y,x^=y,y=c<<1);return x;};

Try it online!
Based on https://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/

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

W, 6 5 bytes

1.MJk

Explanation

  M   % Map the (implicit) input with ...
1.    % generate a list with this length
   J  % Join the array (NOT using the error-proof apply addition.)
    k % Find the length of the array
\$\endgroup\$
0
\$\begingroup\$

Python 2 & 3, 39 30 bytes (function)

lambda x,y:eval("-~"*x+str(y))

Try it online!

The -, * and + are negation, string repetition and string concatenation respectively, not mathematical operators.

Explanation

-~y effectively increments y, so doing -~-~...-~-~y x times increments y x times.


These two are just for fun c:

Python 2, 54  45 bytes (program)

x,y=input(),str(input());print eval("-~"*x+y)

Python 3, 55 46 bytes (program)

x,y=int(input()),input();print(eval("-~"*x+y))
\$\endgroup\$
3
  • \$\begingroup\$ while i was writing that i realized it doesn't work for negative numbers, is that a problem? \$\endgroup\$ Commented Aug 31, 2019 at 13:16
  • \$\begingroup\$ @A_ oh i was thinking about doing that but i ruled it out pretty quickly because i thought the - counted as a mathematical operator; ill edit it \$\endgroup\$ Commented Aug 31, 2019 at 22:49
  • \$\begingroup\$ The unary - is a negation operator, and I don't think that counts as a math operator. \$\endgroup\$
    – user85052
    Commented Aug 31, 2019 at 23:40
0
\$\begingroup\$

APL (Dyalog Classic), 3 bytes

Good ol' base 1...

1⊥⎕

Try it online!

Explanation

  ⎕ Take an input
1⊥  Convert from base-1

APL (Dyalog Classic), 4 bytes

≢⎕/1

Try it online!

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

05AB1E, 2 bytes

Nothing really new. All three are programs are already done in other languages, and simply ported to 05AB1E. Just like all other answers, it assumes positive integers are used.

Convert the (implicit) input-pair from base-1 to an integer:

Try it online.

Loop the first (implicit) input amount of times, and increment that second (implicit) input by 1 each iteration:

F>

Try it online.

(3 bytes) Convert both values in the (implicit) input-pair to a list in the range [1,value]; flatten the list; and take the total length:

L˜g

Try it online.

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

Excel, 15

This should fit the bill, but it's boring.

=SUMPRODUCT(1:1

Inputs go anywhere in row 1.

Assuming just SUM()/SUMIF() is cheating. I'd say it's not, technically, as it's more capable than the + operator (for one thing 1+"" gives an error), but I'm guessing people won't like that.

Other solutions

Regular formula, 15

  • =SUBTOTAL(9,1:1. This is shorter than the AGGREGATE() way.

Matrix Multiplication, 19

  • Inputs are A1, B2
  • Set cell A2 to 1
  • =MMULT(A1:B1,B1:B2
\$\endgroup\$
0
\$\begingroup\$

Octave, 36 bytes

@(a,b)size(conv2(eye(a),eye(b)),1)+1

Try it online!

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

Pascal, 9 B

In Extended Pascal (ISO standard 10206) the built‑in succ and pred functions accept a second argument. To calculate the sum of two integral values write:

succ(x,y)

“But!” you will say:

Additionally, you can't use any built-in functions that are designed to replace those math operators.

succ and pred are not designed to replace +/-. You can write

program sucks;
type
    enumeration = (a, b, c);
var
    x: enumeration value a;
begin
    x := succ(x, 2);
    { x is now c }
end.

but

    x := x + 2;

is illegal. Therefore, succ is not a substitute for +, neither is pred a replacement for . Moreover, succ and pred will refuse operating on real values.

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

Japt, 2 bytes

Takes input as an array and simply converts it from base-1.

ì1

Try it

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

C (gcc), 34 bytes

f(a,b){a=a&b?f(a^b,(a&b)<<1):a^b;}

Try it online!

A probably cheating 13-byte version (from @dave's Java submission):

f(a,b){a+=b;}

Try it online!

\$\endgroup\$
1 2
3

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.