/**
  * Recursive utility method used by Math_Fibonacci::decompose()
  * 
  * @param integer $num
  * @param array $sum array of Fibonacci numbers
  * @return mixed null on success, PEAR_Error otherwise
  * @access private
  */
 function _recDecompose($num, &$sum)
 {
     /*{{{*/
     if (!Math_IntegerOp::isMath_Integer($num)) {
         return PEAR::raiseError('Not a valid Math_Integer object');
     }
     if (!is_array($sum)) {
         $sum = array();
     }
     $n = Math_Fibonacci::_estimateN($num);
     if (PEAR::isError($n)) {
         return $n;
     }
     $fibn = Math_Fibonacci::term($n);
     if (PEAR::isError($fibn)) {
         return $fibn;
     }
     $cmp = Math_IntegerOp::compare($fibn, $num);
     if ($cmp == 0) {
         $sum[] = $fibn;
         return null;
     } elseif ($cmp == -1) {
         $sum[] = $fibn;
         $newnum = Math_IntegerOp::sub($num, $fibn);
         Math_Fibonacci::_recDecompose($newnum, &$sum);
     } elseif ($cmp == 1) {
         $n_1 = Math_IntegerOp::sub($n, new Math_Integer(1));
         if (PEAR::isError($n_1)) {
             return $n_1;
         }
         $fibn_1 = Math_Fibonacci::term($n_1);
         if (PEAR::isError($fibn_1)) {
             return $fibn_1;
         }
         $sum[] = $fibn_1;
         $newnum = Math_IntegerOp::sub($num, $fibn_1);
         Math_Fibonacci::_recDecompose($newnum, &$sum);
     }
 }
Пример #2
0
    $i2 = new Math_Integer('11111');
}
$i3 = new Math_Integer(6);
echo '* Using lib: ' . MATH_INTLIB . "\n";
echo 'i1 = ' . $i1->toString() . "\n";
echo 'i2 = ' . $i2->toString() . "\n";
echo 'i3 = ' . $i3->toString() . "\n";
$res = Math_IntegerOp::add($i1, $i2);
echo 'i1 + i2 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::sub($i1, $i2);
echo 'i1 - i2 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::sub($i2, $i1);
echo 'i2 - i1 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::mul($i1, $i2);
echo 'i1 * i2 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::div($i1, $i3);
echo 'i1 / i3 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::mod($i2, $i3);
echo 'i1 % i3 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::neg($i1);
echo 'neg(i1) = ' . $res->toString() . "\n";
echo 'sign(neg(i1)) = ' . Math_IntegerOp::sign($res) . "\n";
echo 'sign(neg(0)) = ' . Math_IntegerOp::sign(new Math_Integer(0)) . "\n";
echo 'sign(i2) = ' . Math_IntegerOp::sign($i2) . "\n";
echo 'compare(i1, i2) = ' . Math_IntegerOp::compare($i1, $i2) . "\n";
echo 'compare(i3, i3) = ' . Math_IntegerOp::compare($i3, $i3) . "\n";
echo 'compare(i2, i1) = ' . Math_IntegerOp::compare($i2, $i1) . "\n";
$res = Math_IntegerOp::abs(Math_IntegerOp::neg($i2));
echo 'abs(neg(i2)) = ' . $res->toString() . "\n";
// vim: ts=4:sw=4:et:
// vim6: fdl=1: