Code Golf Asked by Downgoat on January 20, 2021
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.
34, 53
102, 1700
48, 38
384, 1440
361, 674
1444, 25270, 216600
0, 0
0
1, 8
8
0
, you can choose whether you want to output it or not.This is code-golf so shortest code in bytes wins!
DU×µLR’⁵*×
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.
Correct answer by Dennis on January 20, 2021
Answered by Razetime on January 20, 2021
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.
Answered by xnor on January 20, 2021
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.
Answered by xnor on January 20, 2021
def p(a,b):
j=0
while b>0:
print`b%10*a`+j*'0';b/=10;j+=1
Answered by Willem on January 20, 2021
{⍺×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
Answered by Alex A. on January 20, 2021
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
Answered by nicael on January 20, 2021
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.
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
Answered by NinjaBearMonkey on January 20, 2021
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!
Answered by Alex A. on January 20, 2021
ᴙíⓢⓜî*$*Ⅹⁿ_
Found a bug while coding the solution to this problem...
// 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
Answered by Mama Fun Roll on January 20, 2021
(a,b)=>[...b+""].reverse().map((d,i)=>10**i*a*d)
(a,b)=>[...b+""].reverse().map((d,i)=>a*d+"0".repeat(i))
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 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>
Answered by user81655 on January 20, 2021
b=arg[2]:reverse()for i=1,#b do print(arg[1]*b:sub(i,i)*10^(i-1))end
Answered by Nikolai97 on January 20, 2021
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
Answered by Adnan on January 20, 2021
.e**Qsb^Tk_w
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)
Answered by isaacg on January 20, 2021
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
Answered by Element118 on January 20, 2021
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!
Answered by nimi on January 20, 2021
ij48-tn:1-P10w^**P
The compiler (5.1.0) works in Matlab and in Octave.
Each number is input on a separate line.
>> matl ij48-tn:1-P10w^**P
> 361
> 674
1444 25270 216600
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
Answered by Luis Mendo on January 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP