PHP大整数求和

来源:互联网 发布:四柱汉诺塔 python 编辑:程序博客网 时间:2024/05/21 06:35

PHP大整数求和

封装一个函数,实现以下两个大整数求和:
123212234563221223947783932 + 123212234563221223947783932 = ?

<?PHP    $a = '123212234563221223947783932';    $b = '123212234563221223947783932';    echo number_add($a,$b);    function number_add($a,$b){        $m = strlen($a);  //  27        $n = strlen($b);        $count = $m>$n?$m:$n;        $result = '';        $flag = 0;        while($count--){  //  27            $t1 = 0;            $t2 = 0;            if($m>0){                $t1 = $a[--$m];  //  25            }            if($n>0){                $t2 = $b[--$n];            }            $t = $t1+$t2+$flag;            $flag = $t/10;            $result = ($t%10).$result;  //  4,依次拼接        }        return $result;    }?>