Output the Partial Products

17

1

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!

Downgoat

Posted 2015-12-30T00:33:20.627

Reputation: 27 116

I'm assuming that the numbers can be strings, right? – Mama Fun Roll – 2015-12-30T01:46:35.587

That 0,0 test case is making it a lot harder. – xnor – 2015-12-30T02:21:15.333

What is the expected outcome for 12, 102? Most answers seem to return 24, 0, 1200. – Dennis – 2015-12-30T03:00:23.717

@Dennis 24, 0, 1200 is fine. I'll specify in the post – Downgoat – 2015-12-30T03:43:02.120

Answers

4

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.

Dennis

Posted 2015-12-30T00:33:20.627

Reputation: 196 637

3I'm guessing the name of this language comes from the fact that it makes everyone feel jelly. – geokavel – 2015-12-30T05:00:06.167

7

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)

isaacg

Posted 2015-12-30T00:33:20.627

Reputation: 39 268

4

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>

user81655

Posted 2015-12-30T00:33:20.627

Reputation: 10 181

3

Lua, 72 68 Bytes

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

Nikolai97

Posted 2015-12-30T00:33:20.627

Reputation: 653

3

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

Alex A.

Posted 2015-12-30T00:33:20.627

Reputation: 23 761

1⍎¨⍕ is pretty clever. – Dennis – 2015-12-30T14:53:48.547

2

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

Element118

Posted 2015-12-30T00:33:20.627

Reputation: 293

2

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

Adnan

Posted 2015-12-30T00:33:20.627

Reputation: 41 965

1

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

Luis Mendo

Posted 2015-12-30T00:33:20.627

Reputation: 87 464

1

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!

nimi

Posted 2015-12-30T00:33:20.627

Reputation: 34 639

You can even do (\b->(x*10^b*).read.pure). – Lynn – 2015-12-30T02:17:41.483

@Mauris: Nice. Thanks a lot! – nimi – 2015-12-30T02:30:56.573

1

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!

Alex A.

Posted 2015-12-30T00:33:20.627

Reputation: 23 761

1

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

NinjaBearMonkey

Posted 2015-12-30T00:33:20.627

Reputation: 9 925

1~~A@#** saves a couple of bytes. – Dennis – 2015-12-30T04:46:09.073

1

Python 2, 61

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

Willem

Posted 2015-12-30T00:33:20.627

Reputation: 1 528

1

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.

xnor

Posted 2015-12-30T00:33:20.627

Reputation: 115 687

0

, 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

Mama Fun Roll

Posted 2015-12-30T00:33:20.627

Reputation: 7 234

0

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

nicael

Posted 2015-12-30T00:33:20.627

Reputation: 4 585

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. – nicael – 2015-12-30T07:49:47.150

0

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.

xnor

Posted 2015-12-30T00:33:20.627

Reputation: 115 687