I made this to multiply an unlimited size of integers together (meaning no decimals)..
This could be useful for those without the BCMath extension.
<?php
function Mul($Num1='0',$Num2='0') {
if(!preg_match("/^\d+$/",$Num1)||!preg_match("/^\d+$/",$Num2)) return(0);
for($i=0;$i<strlen($Num1);$i++) if(@$Num1{$i}!='0') {$Num1=substr($Num1,$i);break;}
for($i=0;$i<strlen($Num2);$i++) if(@$Num2{$i}!='0') {$Num2=substr($Num2,$i);break;}
$Len1=strlen($Num1);
$Len2=strlen($Num2);
$Rema=$Rema2=array();
for($y=$i=0;$y<$Len1;$y++)
for($x=0;$x<$Len2;$x++)
@$Rema[$i++%$Len2].=sprintf('%02d',(int)$Num1{$y}*(int)$Num2{$x});
for($y=0;$y<$Len2;$y++)
for($x=0;$x<$Len1*2;$x++)
@$Rema2[Floor(($x-1)/2)+1+$y]+=(int)$Rema[$y]{$x};
$Rema2=array_reverse($Rema2);
for($i=0;$i<count($Rema2);$i++) {
$Rema3=str_split(strrev($Rema2[$i]));
for($o=0;$o<count($Rema3);$o++)
if($o==0) @$Rema2[$i+$o]=$Rema3[$o];
else @$Rema2[$i+$o]+=$Rema3[$o];
}
$Rema2=strrev(implode($Rema2));
while(strlen($Rema2)>1&&$Rema2{0}=='0') $Rema2=substr($Rema2,1);
return($Rema2);
}
$A='5650175242508133742';
$B='2361030539975818701734615584174625';
printf(" Mul(%s,%s); // %s\r\n",$A,$B, Mul($A,$B));
printf("BCMul(%s,%s); // %s\r\n",$A,$B,BCMul($A,$B)); ?>
It was a fun experience making.. even though this took me longer than the BCAdd alternative I did..
Memory allocation might be an issue for rediculously larger numbers though.. if someone wants to benchmark the performance of my function; feel free.
Enjoy,
Nitrogen.