Multiply two 100-Digit Numbers inside Excel Using Matrix

ε祈祈猫儿з 提交于 2020-01-20 09:36:34

问题


I want to multiply two 100-Digit Numbers In Excel using matrix. The issue in Excel is that after 15-digit, it shows only 0. So, the output also need to be in a Matrix.

1st Number: "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"

2nd Number: "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"

Output: "22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222217777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777778"


回答1:


This may be what OP was after. I thought I would try a naive multiplication method to see how long it would take to run. The answer is less than a second for two 100-digit numbers. You have to select the output range (i.e. A3:GR3 for a 200-digit result) and enter the formula containing the input ranges as an array formula using CtrlShiftEnter e.g.

=Multiply(A1:CV1,A2:CV2)

for two 100-digit numbers.

The method is basically just a simulation of school maths long multiplication, except that the intermediate rows are not stored but immediately added to the answer thus saving a lot of space.

The utility of it is obviously not that it is a replacement for the Karatsuba method, but it is a simple verifiable method which could be used for one-off calculations.

Currently limited to multiplication of rows containing more than one cell (so if you wanted to multiply by a single digit number, would have to enter it as e.g. 09).

Start of numbers

Middle of numbers

End of numbers

Function Multiply(rng1 As Variant, rng2 As Variant)

Dim arr() As Integer
Dim arrLength, r1Length, r2Length, carry, product, digit As Integer
Dim tot, totDigit, totCarry As Integer
Dim v1, v2 As Variant

v1 = rng1
v2 = rng2
r1Length = UBound(v1, 2)
r2Length = UBound(v2, 2)

arrLength = r1Length + r2Length

' Declare 1D array with enough space

ReDim arr(1 To arrLength)

' Loop over digits in first number starting from right

For i = r1Length To 1 Step -1
carry = 0
totCarry = 0

' Loop over digits in second number starting from right

    For j = r2Length To 1 Step -1

' Calculate next digit in intermediate values (i.e. one row of long multiplication)

    product = v1(1, i) * v2(1, j) + carry
    digit = product Mod 10
    carry = Int(product / 10)

' Calculate next digit in final values (i.e. totals line of long multiplication)

    tot = arr(i + j) + digit + totCarry
    arr(i + j) = tot Mod 10
    totCarry = Int(tot / 10)

    Next j

' Process final carry

arr(i) = carry + totCarry
Next i

' Return as an array

Multiply = arr

End Function



回答2:


OK it might work like this:-

(1) You need to concatenate your numbers into a string because that's what you need as the input to your function. Native Excel won't do concatenation on arrays so you need a UDF like this one. So B2 contains

=concat(D1:G1)

(2) The output from the function is a string so you need to split it back into separate cells. You could use another UDF or a formula like this one copied across:-

=IF(COLUMNS($C3:C3)>LEN($B$3),"",VALUE(MID($B3,COLUMNS($C3:C3),1)))

So for the simple example it would look like this:-

But I might have got the wrong end of the stick completely.




回答3:


Place your arrays into A1:A100 and B1:B100, then use three formulas:

1) In C2:C200 enter this as an array formula:

=MMULT(IFERROR(INDEX(A1:A100*TRANSPOSE(B1:B100),(ROW(INDIRECT("1:"&ROWS(A1:A100)*2-1))>0)+TRANSPOSE(ROW(INDIRECT("1:"&ROWS(A1:A100))))-1,MOD(ROW(INDIRECT("1:"&ROWS(A1:A100)*2-1))-TRANSPOSE(ROW(INDIRECT("1:"&ROWS(A1:A100)))),ROWS(A1:A100)*2-1)+1),0),SIGN(A1:A100+1))

2) In D1 enter =C1+INT(D2/10) and fill down to D200.

3) In E1 enter =MOD(D1,10) and fill down to D200.

E1:E200 will contain the answer.



来源:https://stackoverflow.com/questions/32943905/multiply-two-100-digit-numbers-inside-excel-using-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!