17
\$\begingroup\$

In long multiplication, after multiplying the numbers, you are left with the partial products, in this challenge you will output those partial products.

Because long multiplication is long, to compensate your code will need to be as short as possible.

Examples

34, 53
102, 1700

48, 38 
384, 1440

361, 674
1444, 25270, 216600

0, 0
0

1, 8
8

Specifications

  • Input / Output may be in any reasonable format such as array, comma-separated string (or any other delimiter that's not a digit), list, function arguments, etc.
  • Partial products must be in increasing order.
  • If a partial product is 0, you can choose whether you want to output it or not.

This is so shortest code in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ I'm assuming that the numbers can be strings, right? \$\endgroup\$ – Mama Fun Roll Dec 30 '15 at 1:46
  • \$\begingroup\$ That 0,0 test case is making it a lot harder. \$\endgroup\$ – xnor Dec 30 '15 at 2:21
  • \$\begingroup\$ What is the expected outcome for 12, 102? Most answers seem to return 24, 0, 1200. \$\endgroup\$ – Dennis Dec 30 '15 at 3:00
  • \$\begingroup\$ @Dennis 24, 0, 1200 is fine. I'll specify in the post \$\endgroup\$ – Downgoat Dec 30 '15 at 3:43

16 Answers 16

4
\$\begingroup\$

Jelly, 10 bytes

DU×µLR’⁵*×

Try it online!

How it works

DU×µLR’⁵*×  Left argument: multiplier -- Right argument: multiplicant

D           Convert the multiplier to base 10 (array of digits).
 U          Reverse the array.
  ×         Multiply each digit by the multiplicant.
   µ        Begin a new, monadic chain. Argument: A(array of products)
    L       Get the length of A.
     R      Turn length l into [1, ..., l].
      ’     Decrement to yield [0, ..., l-1].
       ⁵*   Compute 10**i for each i in that range.
         ×  Hook; multiply the powers of ten by the corresponding elements of A.
\$\endgroup\$
  • 3
    \$\begingroup\$ I'm guessing the name of this language comes from the fact that it makes everyone feel jelly. \$\endgroup\$ – geokavel Dec 30 '15 at 5:00
7
\$\begingroup\$

Pyth, 12 bytes

.e**Qsb^Tk_w

Test suite

Takes input newline separated, e.g.

361
674

Explanation:

.e**Qsb^Tk_w
                Implicit: Q = eval(input()),T = 10
           w    Input the second number as a string.
          _     Reverse it.
.e              Enumerated map, where b is the character and k is the index.
     sb         Convert the character to an int.
   *Q           Multiply by Q.
  *    ^Tk      Multiply by T ^ k. (10 ^ index)
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES7), 48 bytes

(a,b)=>[...b+""].reverse().map((d,i)=>10**i*a*d)

ES6 (56 bytes)

(a,b)=>[...b+""].reverse().map((d,i)=>a*d+"0".repeat(i))

Explanation

Returns an array of partial products as numbers.

(a,b)=>
  [...b+""]    // convert the multiplier to an array of digits
  .reverse()   // reverse the digits of the multiplier so the output is in the right order
  .map((d,i)=> // for each digit d of the multiplier
    10**i      // get the power of ten of the digit
      *a*d     // raise the product of the digit to it
  )

Test

Test uses Math.pow instead of ** to make it work in standard browsers.

var solution = (a,b)=>[...b+""].reverse().map((d,i)=>Math.pow(10,i)*a*d)
A = <input type="text" id="A" value="361" /><br />
B = <input type="text" id="B" value="674" /><br />
<button onclick="result.textContent=solution(+A.value,+B.value)">Go</button>
<pre id="result"></pre>

\$\endgroup\$
3
\$\begingroup\$

Lua, 72 68 Bytes

b=arg[2]:reverse()for i=1,#b do print(arg[1]*b:sub(i,i)*10^(i-1))end
\$\endgroup\$
3
\$\begingroup\$

APL, 21 bytes

{⍺×x×10*1-⍨⍳≢x←⊖⍎¨⍕⍵}

This is a dyadic function that accepts integers on the left and right and returns an array. To call it, assign it to a variable.

Explanation:

             x←⊖⍎¨⍕⍵} ⍝ Define x to be the reversed digits of the right input
     10*1-⍨⍳≢         ⍝ Generate all 10^(1-i) for i from 1 to the number of digits
{⍺×x×                 ⍝ Multiply the right input by the digits and the powers of 10
\$\endgroup\$
  • 1
    \$\begingroup\$ ⍎¨⍕ is pretty clever. \$\endgroup\$ – Dennis Dec 30 '15 at 14:53
2
\$\begingroup\$

05AB1E, 15 bytes

Code:

VDgUSXFTNmY**=\

Explanation:

VDgUSXFTNmY**=\

V                 # Assign the input to Y
 D                # Duplicate of input, because the stack is empty
  g               # Pushes the length of the last item
   U              # Assign the length to X
    S             # Split the last item
     X            # Pushes X (length of the last item)
      F           # Creates a for loop: for N in range(0, X)
       TNm        # Pushes 10 ^ N
          Y       # Pushes Y (first input)
           *      # Multiplies the last two items
            *     # Multiplies the last two items
             =    # Output the last item
              \   # Discard the last item
\$\endgroup\$
2
\$\begingroup\$

Pyth, 26 bytes

DcGHKjHTFNJlK*G*@Kt-JN^TN

This defines a function c such that it accepts 2 arguments, and the function prints the partial products.

DcGHKjHTFNJlK*G*@Kt-JN^TN
DCGH                      Define function c(G, H)
    KjHT                  Set K to the list of digits converting H to base 10
        FNJlK             Set J to the length of K and loop with variable N
                          (Implicit: print)
             *G*@Kt-JN    Calculates the partial product
                      ^TN Raising it to the appropriate power of 10
\$\endgroup\$
1
\$\begingroup\$

MATL, 18 bytes

ij48-tn:1-P10w^**P

The compiler (5.1.0) works in Matlab and in Octave.

Each number is input on a separate line.

Example

>> matl ij48-tn:1-P10w^**P
> 361
> 674
1444  25270 216600

Explanation

i           % input first number (say 361)
j           % input second number, interpreted as a string (say '674')
48-         % subtract '0' to obtain vector of figures (gives [6 7 4])
tn:1-P      % vector [n-1, ... 1, 0] where n is the number of figures (gives [2 1 0])
10w^        % 10 raised to that, element-wise (gives [100 10 1])
*           % multiply, element-wise (gives [600 70 4])
*           % multiply (gives 361*[600 70 4], or [216600 25270 1444])
P           % flip vector ([1444 25270 216600]). Implicitly display
\$\endgroup\$
1
\$\begingroup\$

Haskell, 60 57 54 bytes

g x=zipWith(\b->(x*10^b*).read.pure)[0..].reverse.show

5 bytes less (drop the .show) if I can take the second number as a string.

Usage example: g 361 674 -> [1444,25270,216600].

Multiply every digit of the reverse of y with x and scale with 10^i where i = 0,1,2,....

Edit: Thanks to @Mauris for 3 bytes!

\$\endgroup\$
  • \$\begingroup\$ You can even do (\b->(x*10^b*).read.pure). \$\endgroup\$ – Lynn Dec 30 '15 at 2:17
  • \$\begingroup\$ @Mauris: Nice. Thanks a lot! \$\endgroup\$ – nimi Dec 30 '15 at 2:30
1
\$\begingroup\$

Julia, 50 49 bytes

f(a,b)=[a*d*10^~-i for(i,d)=enumerate(digits(b))]

This is a function that accepts two integers and returns an integer array.

The digits function returns an array of the input integer's digits in reverse order. We get the index, value pairs using enumerate and compute the partial products as the first input times the digits times 10 raised to the power of the digit's index - 1.

Saved a byte thanks to Dennis!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 61

def p(a,b):
 j=0
 while b>0:
  print`b%10*a`+j*'0';b/=10;j+=1 
\$\endgroup\$
1
\$\begingroup\$

CJam, 19 17 bytes

q~W%eef{~~A@#**}p

Takes input with the first item being an integer and the second a string (e.g. 34 "53"). Suggestions are welcome, as I'm sure it can be shorter. Thanks to Dennis for saving two bytes.

Try it online.

Explanation

q~    e# Get input and evaluate it, x and "y"
W%    e# Reverse y so it will be properly sorted
ee    e# Enumerate through y with each character and its index
f{    e# For each digit in y...
  ~~  e# Convert the digit to an integer on the stack
  A@# e# Take 10 to the power of y's index
  **  e# Multiply all three together to get the final result
}
p     e# Print the array
\$\endgroup\$
  • 1
    \$\begingroup\$ ~~A@#** saves a couple of bytes. \$\endgroup\$ – Dennis Dec 30 '15 at 4:46
1
\$\begingroup\$

Haskell, 37 bytes

a%0=[]
a%b=b`mod`10*a:(a*10)%div b 10

No stringifying, just arithmetic. Recursively prepends the smallest partial product to the rest, where the last digit of b is truncated and a multiplier of 10 is applied. The operator precedence works nicely.

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

𝔼𝕊𝕄𝕚𝕟, 11 chars / 23 bytes (non-competitive)

ᴙíⓢⓜî*$*Ⅹⁿ_

Try it here (Firefox only).

Found a bug while coding the solution to this problem...

Explanation

          // Implicit: î = input 1, í = input 2
ᴙíⓢ      // reverse í and split it into an array
ⓜî*$*Ⅹⁿ_ // multiply î by each individual digit in í and put in previous array
          // implicit output
\$\endgroup\$
0
\$\begingroup\$

Japt, 28 bytes

I=[]Vs w m@IpApY+1 /A*U*X};I

Explanation:

I=[]Vs w m@IpApY+1 /A*U*X};I
I=[]                         //that's simple, init an array I
    Vs                       //take the second input and convert to string
       w                     //reverse it and...
         m@              }   //...map through the chars; X is a char, Y is counter
           Ip............    //push the following value into I...
             ApY+1 /A*U*X    //10^(Y+1)/10*U*X, U is the first input
                           I //output the resulting array
\$\endgroup\$
  • \$\begingroup\$ Due to the bug in the interpreter, have to use ApY+1 /10 instead of ApY, because Ap0 (which is 10^0) gives 100. I guess it's for the reason to allow fast squaring with Ap, but 0 doesn't mean "no arguments". Plz fix, Eth. \$\endgroup\$ – nicael Dec 30 '15 at 7:49
0
\$\begingroup\$

Python 2, 42 bytes

f=lambda a,b:b*[0]and[b%10*a]+f(a*10,b/10)

No stringifying, just arithmetic. Recursively appends the smallest partial product to the rest, where the last digit of b is truncated and a multiplier of 10 is applied.

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