function q53() { $count = 0; for ($n = 1; $n <= 100; ++$n) { for ($k = 0; $k <= $n; ++$k) { $numberOfCombinations = Math::combination($n, $k); if ($numberOfCombinations > 1000000) { ++$count; } } } return $count; }
function q21($from, $to) { $integerToSum = []; for ($i = $from; $i < $to; ++$i) { $primeDecomposition = IntegerFactorization::solve($i); $sumOfDivisors = Math::divisorsSum($primeDecomposition); $sumOfProperDivisors = $sumOfDivisors - $i; $integerToSum[$i] = $sumOfProperDivisors; } $sumOfAmicableNumbers = 0; foreach ($integerToSum as $integer => $sum) { if ($integer !== $sum && isset($integerToSum[$sum]) && $integerToSum[$sum] === $integer) { $sumOfAmicableNumbers += $integerToSum[$integer] + $integerToSum[$sum]; unset($integerToSum[$integer]); unset($integerToSum[$sum]); } } return $sumOfAmicableNumbers; }
function q34() { // The maximum value the sum of factorial function can reach is ceil(log(n, 10)) * 9!, where n is any number // for example, if n = 399, then ceil(log(399, 10)) = ceil(2.6) = 3 (3 digits) and thus the max for 3 digits is // 3 * 9! or 3 * 362880 // ceil(log(n, 10)) * 9! < n // If we ignore the ceil and solve for log(n, 10) * 9! < n, n > 74482 $n = 3; $total = 0; for ($n = 3; $n < 74482; ++$n) { $parts = str_split($n); $sum = 0; foreach ($parts as $part) { $factorial = Math::factorial($part); $sum += $factorial; } if ($sum === $n) { $total += $sum; } } return $total; }
/** * @param array $lexemes ordered from smallest to biggest * @param int $permutationIndex */ function q24(array $lexemes, $permutationIndex) { $lexemesCount = count($lexemes); $size = Math::factorial($lexemesCount); if ($permutationIndex > $size) { throw new LogicException('Index is superior to the size of the generatable permutations.'); } $output = ''; while ($lexemesCount !== 0) { $x = Math::factorial($lexemesCount - 1); foreach ($lexemes as $index => $lexeme) { if ($permutationIndex <= $x) { $output .= $lexemes[$index]; unset($lexemes[$index]); break; } $permutationIndex -= $x; } --$lexemesCount; } echo $output; }
<?php use tomzx\ProjectEuler\Math; require_once 'vendor/autoload.php'; echo Math::combination(40, 20);
/** * @param int $n * @param int $k * @return int */ public static function combination($n, $k) { return Math::factorial($n) / (Math::factorial($k) * Math::factorial($n - $k)); }
function q5($number) { $numbers = range(1, $number); return Math::leastCommonMultiple($numbers); }