1
\$\begingroup\$

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.

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

Ruby, 27 bytes

->i,j{(j!=0)?(i/(1.0/j)):0}
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ What about j=0? \$\endgroup\$ – Sanchises Feb 17 '17 at 8:27
  • \$\begingroup\$ @Sanchises fixed \$\endgroup\$ – anna328p Feb 17 '17 at 8:34
  • \$\begingroup\$ Yes I can see division by zero being an issue unless Ruby returns infinity as an answer \$\endgroup\$ – Shaun Bebbers Feb 17 '17 at 8:36
  • \$\begingroup\$ j=0 is no problem, at least on ruby 2.3. Just try it. \$\endgroup\$ – G B Feb 17 '17 at 9:13
3
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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.) \$\endgroup\$ – Mitchell Spector Feb 17 '17 at 8:38
  • \$\begingroup\$ 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.) \$\endgroup\$ – Greg Martin Feb 17 '17 at 8:41
  • \$\begingroup\$ 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. \$\endgroup\$ – Mitchell Spector Feb 17 '17 at 8:44
0
\$\begingroup\$

Ruby, 22 bytes

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

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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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)

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.