在Excel中使用Matrix乘以两个100位数字 [英] Multiply two 100-Digit Numbers inside Excel Using Matrix

查看:155
本文介绍了在Excel中使用Matrix乘以两个100位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用矩阵在Excel中乘以两个100位数字。 Excel中的问题是,在15位数后,它只显示0.所以输出也需要在一个矩阵中。



第一个数字:99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999



第二数量: 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222



输出: 22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222217777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777778


解决方案

这可能是OP之后的。我以为我会尝试一个天真的乘法方法,看看运行需要多长时间。对于两个100位数字,答案不到一秒钟。您必须选择输出范围(即A3:GR3为200位数的结果),并使用 Ctrl Shift 输入,例如

  =乘法(A1:CV1,A2:CV2)

两个100位数字。



该方法基本上只是一个模拟除了中间行不存储但立即添加到答案中,因此节省了大量空间。



它的效用显然不是它是Karatsuba方法的替代品,但它是一种简单的可验证方法,可用于一次性计算。



目前仅限于包含超过一个单元格(所以如果你想乘以一位数字,就必须输入它,例如09)。



数字开始





中间数字





数字结束





<$ p $函数乘法(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

'声明具有足够空间的一维数组

ReDim ar r(1到arrLength)

'从右边开始的第一个数字的数字循环

对于i = r1Length到1 Step -1
carry = 0
totCarry = 0

'从右边开始的第二个数字的数字循环

对于j = r2Length到1步骤-1

'计算中间值的下一位数(即一行长乘法)

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

'计算最终值中的下一个数字(即长乘法的总和行)

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

下一步j

'进程最终进位

arr(i)= carry + totCarry
Next i

'作为数组返回

乘法= arr

结束函数


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"

解决方案

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

这篇关于在Excel中使用Matrix乘以两个100位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆