Search code examples
excelmatrix-multiplicationmultiplication

Multiply two 100-Digit Numbers inside Excel Using Matrix


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"


Solution

  • 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

    ![enter image description here

    Middle of numbers

    ![enter image description here

    End of numbers

    ![enter image description here

    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