-8
\$\begingroup\$

This challenge is as follows.

(Uhoh no! Now everyone is going to use reciprocal division. Now all the answers are going to look the same and lose originality, ugh!)

Given two integers multiply them, ... but you can't use the multiplication symbols "*" and "x". As you probably know multiplication is just an extension of successive additions, so this challenge is definitely feasible, as for instance the product of 8 and 5 is five sums of 8 or eight sums of 5:

8 * 5 = 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5, & also ...

= 8 + 8 + 8 + 8 + 8 = 40

Restrictions/Conditions:

1) The two numbers are allowed to be any two integers.

2) The calculation should work on both positive and negative integers.

3) The multiplication product should be correct no matter the order in which the two integer factors are presented.

4) You cannot import any installable modules/libraries not native to the base functions of your programming language.

5) There is no need to worry about expanding the scope of either of the integer sizes as it is highly unlikely that either of the integers would overload your system memory.

6) You cannot use either of the operators "*" or "x" as multiplication operators.

7) In case anybody asks, this is multiplication in base 10, exclusively.

8) New Restriction: One multiplication function/operators are now banned. Sorry Jonathon. Creating a new operator is fine, but using any built in one function multiplication operators are banned.

Desired output is the following:

a x b=c

Again, "x" here is not to be used as an operator, just a character.

So if a is 5 and b is 8, then c should be 40, and vice-versa.

This is , so the shortest code in bytes wins! Good luck!

Finally, if you could give a brief description of how your program works, everyone will appreciate that, I'm sure. Thanks.

\$\endgroup\$
  • \$\begingroup\$ Possible duplicates \$\endgroup\$ – Digital Trauma Jul 22 '17 at 2:38
  • \$\begingroup\$ No, it is not a duplicate because in the other challenge the winning condition was the fastest program to calculate the product of two integers. In my question, the winning condition is the least amount of bytes. In the other question that links, addition operators were not allowed. Addition is allowed in my challenge. \$\endgroup\$ – xyz123 Jul 22 '17 at 2:42
  • \$\begingroup\$ Related. \$\endgroup\$ – notjagan Jul 22 '17 at 2:53
  • 3
    \$\begingroup\$ Do X without Y in one of our things to avoid when writing challenges. The reason is primarily that elaborating a spec that doesn't essentially boil down to a worst abuse of the rules contest is very difficult, as evidenced by the answers that simply divide by the multiplicative inverse. \$\endgroup\$ – Dennis Jul 22 '17 at 3:13
  • \$\begingroup\$ 'built in one function multiplication operators are banned' : so an operator that takes a vector and returns the product of its elements would be banned? \$\endgroup\$ – Giuseppe Jul 22 '17 at 5:04
3
\$\begingroup\$

J, 2 bytes

%%

x * y = x / (1 / y)

%%  Input: x (LHS), y (RHS)
 %  Reciprocal of y
%   Divide x by 1/y

Also, multiplication is addition after log, then computing the power using base e.

5 bytes

+&.^.

x * y = e^(log(x * y)) = e^(log(x) + log(y))

+&.^.  Input: x (LHS), y (RHS)
 & ^.  Natural log of x and y
+      Add them
 &.^.  Apply inverse of natural log to the sum
       NB. e^sum
\$\endgroup\$
  • \$\begingroup\$ 0 == 0 (%%) 0 It does work @JonathanAllan \$\endgroup\$ – Conor O'Brien Jul 22 '17 at 3:04
  • \$\begingroup\$ Also, 4 bytes: 1#.# \$\endgroup\$ – Conor O'Brien Jul 22 '17 at 3:05
  • \$\begingroup\$ Well it works in Jelly as anything divided by inf is 0... \$\endgroup\$ – Jonathan Allan Jul 22 '17 at 3:06
  • \$\begingroup\$ What it really does work with zeroes!? Huh, if that's true then it looks like you are winning! \$\endgroup\$ – xyz123 Jul 22 '17 at 3:08
2
\$\begingroup\$

Python 2, 31 bytes

lambda x,y:y and x/(1/float(y))

Try it online!

Old approach was longer, but then I remembered Math!:

Python 2, 49 bytes

lambda x,y:sum([x,-x][y<0]for i in range(abs(y)))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ What happens if y is 0? \$\endgroup\$ – Digital Trauma Jul 22 '17 at 3:02
  • \$\begingroup\$ For the second one I think you can remove the space after [y<0] \$\endgroup\$ – Conor O'Brien Jul 22 '17 at 3:08
  • 1
    \$\begingroup\$ For the 30 byter add y and to cater for when y is 0 and the division errors. Also I think it's acceptable to return the float (get rid of int()) since the result will still be correct. Finally move to Python 3 and do away with the float() Try It Online! for 24 bytes. \$\endgroup\$ – Jonathan Allan Jul 22 '17 at 3:18
  • \$\begingroup\$ Fixed divide by 0 error (thx Jonathon Allan; and 1byte saved in longer version (thx Conor O'Brien ) \$\endgroup\$ – Chas Brown Jul 22 '17 at 4:14
  • \$\begingroup\$ If you want to stick to Python 2 you can use 1./y to save six bytes. \$\endgroup\$ – Neil Jul 22 '17 at 10:33
2
\$\begingroup\$

Jelly, 12 3 bytes

Uh turns out this works - port of Mile's J answer

İ÷@

A dyadic link taking the two integers

Try it online!

How?

İ÷@ - Link: a, b
İ   - inverse a (i.e. 1/a - note: if a is 0 this yields inf)
 ÷@ - divide with reversed arguments (if a was 0, this is b divided by inf, which yields 0)

...which sure beats this:

Aẋ/SN¹⁸ṠSỊ¤?

A monadic link taking a list of the two integers and returning their product.

How?

Aẋ/SN¹⁸ṠSỊ¤? - Link: list [a, b]    e.g. [-7, -3]     [3, -7]          [7, 3]    or [3, 0]
A            - absolute values           [ 7,  3]     [3,  7]          [7, 3]       [3, 0]
 ẋ/          - reduce by repetition      [ 7, 7, 7]   [3,3,3,3,3,3,3]  [7, 7, 7]    []
   S         - sum                        21           21               21           0
           ? - if:
          ¤  -   nilad followed by link(s) as a nilad:
      ⁸      -     chain's left argument [-7, -3]     [3, -7]          [7, 3]       [3, 0]
       Ṡ     -     sign                  [-1, -1]     [1, -1]          [1, 1]       [1, 0]
        S    -     sum                    -2           0                2            1
         Ị   -     abs(v) <= 1?            0           1                0            1
    N        - then: negate                           -21                  negate(0)=0
     ¹       - else: do nothing           21                            21
\$\endgroup\$
1
\$\begingroup\$

Ly, 17 bytes

n1-<ns>[<l+>1-]<u

A more difficult golf than I imagined...

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

JavaScript (ES7), 14 bytes

(x,y)=>x/y**-1
\$\endgroup\$
  • \$\begingroup\$ Don't use '*' . \$\endgroup\$ – 0x45 Jul 22 '17 at 10:39
  • 1
    \$\begingroup\$ @0x45 I'm not using *, I'm using **, which is a different operator. \$\endgroup\$ – Neil Jul 22 '17 at 11:39
  • \$\begingroup\$ Save a byte with currying. \$\endgroup\$ – Shaggy Jul 22 '17 at 11:42
0
\$\begingroup\$

Brainfuck, 32 bytes

,>,[-<[->>+>+<<<]>>[-<<+>>]<]>>.
,>, take ascii input and put it in the first and second cells

[-  until the second cell is zero

    <[->>+>+<<<]> move the first cell value into the third and fourth cells

    >[-<<+>>]<    move the third cell to the first one again

] end loop. This layers the first value onto the fourth cell,
  as many times as the value in the second cell (= product of 1st & 2nd)

>>. print the fourth cell
\$\endgroup\$
0
\$\begingroup\$

Japt, 4 bytes

/VpJ

Test it


Explanation

Same as Neil's JS solution; divides (/) the first integer (U, implicit) by the second integer (V) raised to the power (p) of -1 (J).

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

R, 20 bytes

function(a,b)a/(1/b)

in R, a/Inf is 0. Moreover, in the R, we can actually see define * to be this operation instead of .Primitive("*") which is the standard multiplication operator. The TIO link shows the old and new values and demonstrates that we can use our new * operator as the infix multiplication operator.

Try it online!

3 Bytes:

%*%

the matrix multiplication operator. Implicitly converts numbers into matrices, so it returns a 1x1 matrix as a result, but that's stored as a length-1 vector in R, just like a single number is.

OR

%o%

Which is an alias for outer(x,y,'*') which also computes the correct value for numbers.

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

Forth, 7 bytes ( But it's a cheat. )

: m * ; ( Renaming it is still using it. )

use:
10 11 m . 110  ok

Forth, 37 bytes ( the suggested loop )

: c 0 swap 0 ?do over + loop . drop ; 

Use:
2 2 c  4  ok
0 0 c 0  ok
0 1 c 0  ok
1 0 c 0  ok
1 1 c 1  ok
1 2 c 2  ok
2 1 c 2  ok
1001 2002 c 2004002  ok
\$\endgroup\$

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.