# Multiply without multiplying, without loops either [closed]

Make a function, method, program or routine that will accept two interger parameters (say x and y) and the total of the two values multiplied. Except straight multiplication is not allowed so you must not simply do something like:

int multiply (int x, int y) {
return x*y;
}


Also you must not use a for () loop, like this:

int multiply (int x, int y) {
int z = 0;
for (int i = 0;i < y; i++){
z += x;
}
return z;
}


Everything else is up to you. A winner is you with the most compact solution.

• Do X without Y is generally considered a bad idea for a challenge. – ATaco Feb 17 '17 at 8:22
• "What about division?"... from above "Everything else is up to you" so what about division? – Shaun Bebbers Feb 17 '17 at 8:33
• @ShaunBebbers ATaco is referring to this. – Martin Ender Feb 17 '17 at 8:50
• But then the second snippet would produce a wrong result for negative inputs. So it's not really illustrative – Luis Mendo Feb 17 '17 at 10:50
• Again, it's a misleading example, as it produces wrong results. I think it should be deleted, or modified to account for the sign – Luis Mendo Feb 17 '17 at 11:16

## Ruby, 27 bytes

->i,j{(j!=0)?(i/(1.0/j)):0}

• What about j=0? – Sanchises Feb 17 '17 at 8:27
• @Sanchises fixed – anna328p Feb 17 '17 at 8:34
• Yes I can see division by zero being an issue unless Ruby returns infinity as an answer – Shaun Bebbers Feb 17 '17 at 8:36
• j=0 is no problem, at least on ruby 2.3. Just try it. – G B Feb 17 '17 at 9:13

# Mathematica, 12 bytes

Exp@*Tr@*Log


Pure function taking a list of numbers (even complex numbers) as input and returning a number. Takes the logarithm of all the numbers in the list, adds them together, and exponentiates the result. Rules of exponents and logarithms ftw! (And because Mathematica calculates Log[0] as -∞ and can subsequently manipulate that symbolically, it arrives at the correct answer if one input number equals 0.)

# Mathematica, 24 bytes

Tr[1^Flatten@Array[,#]]&


Pure function taking a list of nonnegative (unfortunately) integers as input and returning their product. Simply creates an array of the appropriate dimensions and counts the number of entries (using Tr[1^...] as a golfy shorthand for Length@...). Since we don't care what's actually in the array, we can fill it with Nulls using Array[,#], which doesn't cost any bytes for the first argument.

• But how can you compute exponents and logarithms without using multiplication internally? (That's the issue with this sort of challenge -- the rules need to be specified in detail.) – Mitchell Spector Feb 17 '17 at 8:38
• Quite a good point. Mathematica doesn't actually compute the decimal expansion of say Log[2] unless you make it; it just knows how Log and Exp interact with each other. But of course, a transformation rule like Exp[Log[a]+Log[b]] = a*b uses multiplication behind the scenes. (The OP says "straight multiplication" isn't allowed, so presumably this is? But yeah, I agree with your issue.) – Greg Martin Feb 17 '17 at 8:41
• I think this follows the OP's rules -- you don't literally use multiplication in your program. I just think that the challenge could be worded in a way that would make it more interesting. – Mitchell Spector Feb 17 '17 at 8:44

## Ruby, 22 bytes

->a,b{eval ?a<<42<<98}


Maybe bending the rules? This is no "straight" multiplication.

# Perl, 19 bytes

18 bytes of code + -p flag.

s/.*/"\$&+"x<>.0/ee


Try it online!

It takes two numbers (say m and n) and converts them into m+m+...+m+0 (n times) and evaluates this string. (the final +0 is to make sure that if one of the number is 0, then the result is 0 and not an empty string)

# Python, 48 39 bytes

-9 bytes thanks to @ovs

f=lambda x,y,t=0:x and f(x-1,y,t+y)or t


Recursively adds y to t, and returns the sum of t when x is 0.

• lambda a,b:eval(("+a"*b)[1:]) would work. This won't work when b is 0 though. – Yytsi Feb 17 '17 at 8:50
• Would that not also count as "straight multiplication"? – Trelzevir Feb 17 '17 at 8:55
• I don't know :/ Might as well be. – Yytsi Feb 17 '17 at 8:58