使用PHP中的巨号

时间:2013-10-01 07:01:45

标签: php math package largenumber

早上的家伙。 我正在寻找一种方式/扩展,以便我可以用PHP中的大数字计算FAST,例如乘以50位数字。

请不要告诉我,我将使用BC Math或GMP。 他们是我的第二个想法, 但两者都是如此无用,当结果超过38位时,只返回“假”。

我已经尝试过编写自己的计算函数,这实际上是有效的,但我相信它有点慢。 它正在保存数组中数字的每个数字,并且像我在小学里学到的那样计算。

只有当传入的数字也是数组时,它才能正常工作。 因为导致计算机在他保存之前会破坏它们。

有没有办法让我的函数更快一点,或者是否存在一个真正用bignumber计算的lib?

function multiplicate($A, $B)
{
    if (!is_array($A))
    {
        $A = preg_split("//", strval($A), -1, PREG_SPLIT_NO_EMPTY);
    }
    if (!is_array($B)) 
    {
        $B = preg_split("//", strval($B), -1, PREG_SPLIT_NO_EMPTY);
    }

    if ( $A[0]=='-') 
    {
        array_splice($A, 0, 1);
        $negA=-1;
    }
    else 
    {
        $negA=1;
    };
    if ( $B[0]=='-') 
    {
        array_splice($B, 0, 1);
        $negB=-1;
    }
    else 
    {
        $negB=1;
    };

    $l = 0;
    $m = 0;
    $Erg[0] = 0;

    for ( $i=0; $i < count($A); $i++) 
    {
        for ( $j=0; $j < count($B); $j++) 
        {
            $ZwErg = preg_split("//", strval($A[$i]*$B[$j]), -1, PREG_SPLIT_NO_EMPTY);      
            if ($i==0&&$j==0&&count($ZwErg)==2) 
            {
                $l = 1;
            }
            if (count($ZwErg)==2)
            {
                $m = -1;
            }
            for ( $k=0; $k < count($ZwErg); $k++) 
            {
                $s = $i + $j + $l + $m + 1;
                if (isset($Erg[$s])) 
                {
                    $Erg[$s] = intval($Erg[$s]) + intval($ZwErg[$k]);

                    for ( $n=$s; $n >= 0; $n--)
                    {
                        if (intval($Erg[$n])>9) 
                        {
                            $Erg[$n-1] = intval($Erg[$n-1]) + 1 ;
                            $Erg[$n] = intval($Erg[$n] - 10);
                        }
                     };
                }
                else
                {
                    $Erg[$s] = $ZwErg[$k];
                }
                $m = 0;
            }
        }           
    }
    if ($Erg[0]==0) 
    {
        array_splice($Erg,0,1);
    }

    if ($negA*$negB==-1) 
    {
        array_splice($Erg,0,0,"-"); 
    }
    //$Erg = implode("", $Erg);
    return $Erg;
}

$time_start = microtime(true);

require'mathlab.php';

$a=array(1,2,3,4,3,1,0,9,5,8,7,6,4,2,5,6,1,2,3,9,5,2,6,7,9,0,4,6,3,9,5,2,6,7,9,0,4,6,7,1,2,3,4,3,1,0,9,5,8,7,6,4,2,5,6,7,8,9,0,3,8,1,5,2,8,1,4,0,7,1,2,3,4,3,1,0,9,5,8,7,6,4,2,5,6,7,8,9,0,3,8,1,5,2,8,1,4,0,3,4,3,1,0,9,5,8,7,6,4,2,5,6,7,8,9,0,7,8,9,0); // 100-digits

$b=array(3,9,5,2,6,7,9,0,4,6,7,1,2,3,4,3,1,0,9,5,8,7,3,9,5,2,6,7,9,3,9,5,2,6,7,9,0,4,6,7,1,2,3,4,3,1,0,9,5,8,7,6,4,2,5,6,7,8,9,0,3,8,1,5,2,8,1,4,0,0,4,6,7,1,2,3,4,3,1,0,9,5,8,7,6,4,2,5,6,7,8,9,0,3,8,1,5,2,8,1,4,0,6,4,2,5,6,7,8,9,0,3,8,1,5,2,8,1,4,0); // 100-digits

$a2=123431095876425612395267904639526790467123431095876425678903815281407123431095876425678903815281403431095876425678907890;

$b2=395267904671234310958739526793952679046712343109587642567890381528140046712343109587642567890381528140642567890381528140;

$d=multiplicate($a, $b);

$time_end = microtime(true);

$time = $time_end - $time_start;

$erg= implode("", $d);

echo $erg;  // returns 48788350638348981414888625840026024706504222587793349182466850334644805413224925671434030836485835140547446449665876539897036829747577622276768694829991017552834308659985345098432904112370002027411248553582029194707890154740774064503024600

echo $time; // returns 0.75173783302307

$erg3=bcmul($a2, $b2);

$erg4=bcmul(123431095876425612395267904639526790467123431095876425678903815281407123431095876425678903815281403431095876425678907890, 395267904671234310958739526793952679046712343109587642567890381528140046712343109587642567890381528140642567890381528140);

$erg2=gmp_mul($a2, $b2);

$erg5=gmp_mul(123431095876425612395267904639526790467123431095876425678903815281407123431095876425678903815281403431095876425678907890, 395267904671234310958739526793952679046712343109587642567890381528140046712343109587642567890381528140642567890381528140);


echo gmp_strval($erg2); // returns 0

echo gmp_strval($erg5); // returns 0

echo $erg3; // returns 0

echo $erg4; // returns 0

1 个答案:

答案 0 :(得分:2)

使用bcmathGMP

例如,

echo bcmul("123431095876425612395267904639526790467123431095876425678903815281407123431095876425678903815281403431095876425678907890",
           "395267904671234310958739526793952679046712343109587642567890381528140046712343109587642567890381528140642567890381528140");

给出输出:

48788350638348981414888625840026024706504222587793349182466850334644805413224925671434030836485835140547446449665876539897036829747577622276768694829991017552834308659985345098432904112370002027411248553582029194707890154740774064503024600