Question

Morning Guys. I'm looking for a way/extension, so that I can calculate FAST with really big numbers in PHP, e.g. multiplication with 50-digit numbers.

And please don't tell me, I shall use BC Math or GMP. They were my second idea, but both are so useless, when the result has more than 38 digits both just returning "false".

I allready tried writing an own calculation function, which is actually working, but I believe it's a bit to slow. It's saving every digit of a number in an array, and calcs like I learned it in Elementary school.

it's working perfect only when the incoming numbers are arrays too. cause elseway the computer will break them before he can save them.

Is there a way to make my function a bit faster, or does there exist a lib which really calcs with bignumbers?

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
Was it helpful?

Solution

Use bcmath or GMP.

For example,

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

Gives the output:

48788350638348981414888625840026024706504222587793349182466850334644805413224925671434030836485835140547446449665876539897036829747577622276768694829991017552834308659985345098432904112370002027411248553582029194707890154740774064503024600

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top