Hi, i recently ran into an issue of dealing with big integers. Too big for the type int in PHP ...yeah, sorry it's not c++ but close enough i figure. I was more or less wondering if there was a more efficient wayt to do this without compiling php with the Large numbers libraries.
function MultiplyIntStrings($a, $b)
{
$aLength = strlen($a);
$bLength = strlen($b);
$ExistingCarry = 0;
$AdditionalLen = 1;
if(ord($a[0])*ord($b[0]) > 9) $AdditionalLen++;
$ResultLength = (($aLength + $bLength)-2) + $AdditionalLen;
for($i = 0; $i < $ResultLength; $i++)
$ResultSet[] = 0;
for($k=1, $n=0, $i = $bLength - 1; $i > -1; $i--, $n++)
{
$ExistingCarry = 0;
for($j = $aLength - 1; $j > -1; $j--, $k++)
{
$temp = (integer)$b[$i] * (integer)$a[$j] + $ExistingCarry;
$ExistingCarry = 0;
if($temp > 9)
{
$Remainder = $temp % 10;
$ExistingCarry = ($temp - $Remainder)/10;
}
else
{
$Remainder = $temp;
}
$ResultSet[$ResultLength - $k - $n] += (integer)$Remainder;
}
if($j == -1)
{
$ResultSet[$ResultLength - $k - $n] += (integer)$ExistingCarry;
$k = 1;
}
}
for($i = $ResultLength - 1; $i > -1; $i--)
{
if($ResultSet[$i] > 9)
{
$a = ($ResultSet[$i] % 10);
$ResultSet[$i-1] += (($ResultSet[$i]- $a)/10);
$ResultSet[$i] = $a;
}
}
$i = 0;
while($ResultSet[$i] == 0){ $i++; }
$val = implode('', $ResultSet);
$val = substr($val, $i, strlen($val)-$i);
return $val;
} 