Friday, February 27, 2009

multiply, power function with large number

function power($a,$b){
  if($b==1)return $a;
  return multiply($a,(power($a,$b-1)));
}
function multiply($a,$b){
  $a = ''.$a;
  $b = ''.$b;
  $b_length = strlen($b);
  $value = '';
  for($i=1;$i<=$b_length;$i++){
    $b2 = $b[$b_length-$i];
    $mul = $a*$b2+$temp;
    $temp = floor($mul/10);
    $value = ($mul%10).$value;
  }
  $value = $temp.$value;
  return $value;
}
with both $a and $b is string strlen($a) == 1