快速乘法非常大的整数

时间:2010-01-03 22:48:56

标签: c# java integer multiplication

如何将两个超过32个字符的非常大的数相乘,例如乘以100! 122!或者22 ^ 122与11 ^ 200借助分而治之,做任何身体都有java代码或C#代码吗?

4 个答案:

答案 0 :(得分:3)

您应该使用java.math.BigInteger。这允许整数值的表示远远超过2 ^ 32或甚至2 ^ 64。 BigInteger值基本上仅受程序可用内存量的限制,即32位系统上的大约4 GB,以及64位系统的可用物理+虚拟内存。

import java.math.BigInteger;

class Foo
{
    public static void main(String args[])
    {
        BigInteger bigInteger100Fact = bigFactorial(BigInteger("100")); //where bigFactorial is a user-defined function to calculate a factorial
        BigInteger bigIntegerBar = new BigInteger("12390347425734985347537986930458903458");

        BigInteger product = bigIntegerFact.multiply(bigIntegerBar);
    }
}

编辑:如果你需要一个

,这是一个BigInteger factorial function

答案 1 :(得分:2)

使用Karatsuba和Toom-Cook的java.lang.BigInteger的

Here is a patched version

And here is a Java class可以使用Schönhage-Strassen繁殖BigIntegers:

答案 2 :(得分:1)

Here's some integer multiplication algorithms

Here's a class library for numbers

它包括用于乘以大整数的Karatsuba和Schonhage-Strassen算法。

答案 3 :(得分:0)

我自己写了一个使用Arrays来实现这一目的,只是为了好玩。我相信Java的BigInteger类也会做同样的事情。

Here是C#中可能对您有用的示例。