0

早安伙计们。我正在寻找一种方法/扩展,以便我可以在 PHP 中用非常大的数字计算 FAST,例如与 50 位数字相乘。

请不要告诉我,我将使用 BC Math 或 GMP。它们是我的第二个想法,但是当结果超过 38 位时,它们都只是返回“false”,所以它们都没有用。

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

只有当传入的数字也是数组时,它才能完美运行。因为否则计算机会在他保存它们之前破坏它们。

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

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
4

1 回答 1

2

使用bcmathGMP

例如,

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

给出输出:

48788350638348981414888625840026024706504222587793349182466850334644805413224925671434030836485835140547446449665876539897036829747577622276768694829991017552834308659985345098432904112370002027411248553582029194707890154740774064503024600

于 2013-10-01T07:26:19.003 回答