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; }
/** * @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)); }