Example #1
0
 /**
  * Return the factorial (n!) of the parameter $wanted.
  *
  * If GMP is installed, GMP will be used to calculate the factorial,
  * If libbcmath is installed, bcmath will be used for all mathematics operation.
  *
  * If none of previous library is available, then basic php functions will be used,
  * so the function could result in an error or php can return INF.
  *
  * If GMP is not installed, be aware of the "Maximum function nesting level" value.
  *
  * @param int|string $wanted The wanted factorial base
  *
  * @return int|string
  *
  * @throws \InvalidArgumentException if [$wanted] is not a positive number
  */
 public static function factorial($wanted)
 {
     // Check input data
     if (!is_numeric($wanted) || $wanted < 0) {
         throw new \InvalidArgumentException('The [wanted] parameter MUST be a positive number');
     }
     // Use GMP extension if available
     if (function_exists('gmp_fact')) {
         return gmp_strval(gmp_fact($wanted));
     }
     // Use cache if exist (for performance)
     if (array_key_exists($wanted, self::$factorialCache)) {
         return self::$factorialCache[$wanted];
     }
     if ($wanted === 1 || $wanted === 0) {
         $value = 1;
     } else {
         if (function_exists('gmp_mul')) {
             $value = gmp_strval(gmp_mul($wanted, self::factorial($wanted - 1)));
         } elseif (function_exists('bcmul')) {
             $value = bcmul($wanted, self::factorial($wanted - 1));
         } else {
             $value = $wanted * self::factorial($wanted - 1);
         }
     }
     // Save in cache for later use
     self::$factorialCache[$wanted] = $value;
     return $value;
 }
Example #2
0
 public static function combinations($num, $repetitions)
 {
     if ($num >= $repetitions) {
         return gmp_strval(gmp_fact($num)) / (gmp_strval(gmp_fact($repetitions)) * gmp_strval(gmp_fact($num - $repetitions)));
     } else {
         exit('Error you could, not second parameter that is greater than the first one.');
     }
 }
Example #3
0
 /**
  * Gets the probaibiliy using the pmf function of a certain outcome
  * @param float $x Outcome
  * @return float
  * @throws UnexpectedValueException if x is not an int
  */
 public function get_probability($x)
 {
     if (is_int($x)) {
         return gmp_fact($this->n) / (gmp_fact($x) * gmp_fact($this->n - $x)) * exp($this->p, $x) * exp(1 - $this->p, $this->n - $x);
     } else {
         throw new UnexpectedValueException('UnexpectedValueException occured because BionomailDistribution is not continous so x cannot be a non-integer');
     }
 }
Example #4
0
 public function get_probability($x)
 {
     if (is_numeric($x)) {
         return exp($this->lambda, $x) * exp(M_E, $this->lambda * -1) / gmp_fact($x);
     } else {
         throw new UnexpectedValueException();
     }
 }
function solve($reader)
{
    fscanf($reader, "%d\n", $number);
    for ($i = 0; $i < $number; $i++) {
        fscanf($reader, "%d\n", $N);
        $fac = gmp_strval(gmp_fact($N));
        $sum = 0;
        while ($fac != 0) {
            $sum += bcmod($fac, 10);
            $fac = bcdiv($fac, 10);
        }
        echo $sum . "\n";
    }
}
function CantorExpand($n, $x)
{
    //參數說明
    // $n - 排列的位數
    // $x - 第幾個 (必須要是字串)
    //1. $x 先減1
    $x = gmp_sub($x, "1");
    $str = "";
    for ($i = 1; $i <= $n; $i++) {
        //2. 先算($n-$i)階乘
        $fac = gmp_strval(gmp_fact($n - $i));
        //3. 再算 $x / ($n-$i) 的商跟餘數
        $res = gmp_div_qr($x, $fac);
        $quotient = gmp_strval($res[0]);
        $remainder = gmp_strval($res[1]);
        //4. 比這個位數小的數目總共有 $quotient 個,所以開始找出這個位數
        $str .= findMax($quotient);
        //5. 把餘數設為 $x,下次要使用
        $x = $remainder;
    }
    return $str;
}
Example #7
0
<?php

//      Name  ( Cost, Damage, Armor )
$weapons = array("Dagger" => array(8, 4, 0), "Shortsword" => array(10, 5, 0), "Warhammer" => array(25, 6, 0), "Longsword" => array(40, 7, 0), "Greataxe" => array(74, 8, 0));
$armor = array("Leather" => array(13, 0, 1), "Chainmail" => array(31, 0, 2), "Splintmail" => array(53, 0, 3), "Bandedmail" => array(75, 0, 4), "Platemail" => array(102, 0, 5));
$rings = array("Damage +1" => array(25, 1, 0), "Damage +2" => array(50, 2, 0), "Damage +3" => array(100, 3, 0), "Defense +1" => array(20, 0, 1), "Defense +2" => array(40, 0, 2), "Defense +3" => array(80, 0, 3));
$everything = array_values(array_merge($weapons, $armor, $rings));
$boss = array("Hit Points" => 104, "Damage" => 8, "Armor" => 1);
$hero = array("Hit Points" => 100, "Damage" => 0, "Armor" => 0);
$gold_spent = false;
$permutations = gmp_strval(gmp_fact(count($everything)) / gmp_fact(count($everything) - 3));
// 3 types of items
function permute($items, $perms = array())
{
    if (empty($items)) {
        var_dump($perms);
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            permute($newitems, $newperms);
        }
    }
}
permute($everything);
while ($boss['Hit Points'] > 0 & $hero['Hit Points'] > 0) {
    $damage_to_hero = $boss["Damage"] - $hero["Armor"];
    $damage_to_boss = $hero["Damage"] - $boss["Armor"];
    $boss["Hit Points"] -= $damage_to_boss > 0 ? $damage_to_boss : 1;
Example #8
0
 private function _fact($num)
 {
     $num = (int) $num;
     if (0 > $num) {
         return NAN;
     }
     if (function_exists('gmp_fact')) {
         $fact = gmp_strval(gmp_fact("{$num}"));
     } else {
         $fact = 1;
         for ($i = 2; $i <= $num; ++$i) {
             $fact = $fact * $i;
         }
     }
     return $fact;
 }
Example #9
0
File: 34.php Project: arjona/euler
<?php

$totsum = 0;
// the maximum for a digit is 9! = 362880
// the maximum total for a 7 digit number is 2,540,160
// the maximum total for a 8 digit number is 2,903,040
// our ceiling will have to be 7 digits and no more than 2.9 million
for ($num = 10; $num < 2903040; $num++) {
    $num = (string) $num;
    $sum = 0;
    for ($i = 0; $i < strlen((string) $num); $i++) {
        $sum = gmp_add(gmp_fact($num[$i]), $sum);
    }
    $sum = gmp_strval($sum);
    if ($sum == $num) {
        $totsum += $sum;
        echo "{$sum}\n";
    }
}
echo "total: {$totsum}\n";
function extraLongFactorial($n)
{
    $fact1 = gmp_fact($n);
    // 5 * 4 * 3 * 2 * 1
    return gmp_strval($fact1) . "\n";
}
Example #11
0
 /**
  * Calculates factorial of this value.
  *
  * @return BigInteger
  */
 public function factorial() : BigInteger
 {
     $calculatedValue = gmp_fact($this->getValue());
     return $this->assignValue($calculatedValue);
 }
Example #12
0
File: 20.php Project: arjona/euler
<?php

$fact = gmp_strval(gmp_fact(100));
$sum = 0;
for ($a = 0; $a < strlen($fact); $a++) {
    $sum += $fact[$a];
}
echo "{$sum}\n";
Example #13
0
<?php

/**
 * Lattice paths
 * 
 * Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
 * 
 * How many such routes are there through a 20×20 grid?
 */
$steps_left = 20;
$steps_down = 20;
$possible = 1;
$steps = $steps_left + $steps_down;
while ($steps > $steps_left) {
    $possible = $possible * $steps;
    $steps--;
}
$paths = $possible / gmp_strval(gmp_fact($steps_down));
echo $paths;
 public function factorial($a)
 {
     $result = null;
     $aa = gmp_fact($a);
     $result = gmp_strval($aa);
     //  $this->op = "!";
     return $result;
 }
Example #15
0
var_dump($res);
printf("Result is: q - %s, r - %s" . PHP_EOL, gmp_strval($res[0]), gmp_strval($res[1]));
// gmp_div_r
$div = gmp_div_r("105", "20");
echo gmp_strval($div) . "\n";
// gmp_div
$div1 = gmp_div("100", "5");
echo gmp_strval($div1) . "\n";
// gmp_divexact
$div1 = gmp_divexact("10", "2");
echo gmp_strval($div1) . "\n";
// gmp_fact
$fact1 = gmp_fact(5);
// 5 * 4 * 3 * 2 * 1
echo gmp_strval($fact1) . "\n";
$fact2 = gmp_fact(50);
// 50 * 49 * 48, ... etc
echo gmp_strval($fact2) . "\n";
// gmp_gcd
$gcd = gmp_gcd("12", "21");
echo gmp_strval($gcd) . "\n";
// gmp_gcdext
$a = gmp_init(12);
$b = gmp_init(21);
$g = gmp_gcd($a, $b);
$r = gmp_gcdext($a, $b);
$check_gcd = gmp_strval($g) == gmp_strval($r['g']);
$eq_res = gmp_add(gmp_mul($a, $r['s']), gmp_mul($b, $r['t']));
$check_res = gmp_strval($g) == gmp_strval($eq_res);
if ($check_gcd && $check_res) {
    $fmt = "Solution: %d*%d + %d*%d = %d\n";
Example #16
0
<?php

var_dump(gmp_strval(gmp_fact(0)));
var_dump(gmp_strval(gmp_fact("")));
var_dump(gmp_strval(gmp_fact("0")));
var_dump(gmp_strval(gmp_fact("-1")));
var_dump(gmp_strval(gmp_fact(-1)));
var_dump(gmp_strval(gmp_fact(1.1)));
var_dump(gmp_strval(gmp_fact(20)));
var_dump(gmp_strval(gmp_fact("50")));
var_dump(gmp_strval(gmp_fact("10")));
var_dump(gmp_strval(gmp_fact("0000")));
$n = gmp_init(12);
var_dump(gmp_strval(gmp_fact($n)));
$n = gmp_init(-10);
var_dump(gmp_strval(gmp_fact($n)));
var_dump(gmp_fact());
var_dump(gmp_fact(1, 1));
var_dump(gmp_fact(array()));
var_dump(gmp_strval(gmp_fact(array())));
echo "Done\n";
Example #17
0
<?php

/**
 * Factorial digit sum
 * 
 * n! means n × (n − 1) × ... × 3 × 2 × 1
 * 
 * For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
 * and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
 * 
 * Find the sum of the digits in the number 100!
 */
$factorial = gmp_strval(gmp_fact(100));
$numbers_arr = array();
while ($factorial > 0) {
    $numbers_arr[] = gmp_strval(gmp_mod($factorial, 10));
    $factorial = gmp_strval(gmp_div_q($factorial, 10));
}
echo array_sum($numbers_arr);