* @git WEBD-325-45 * @license GNU General Public License version 2 or later; see LICENSE.txt */ /** * To print out the whole factorial sum * * @param int $n * * @return mixed * * @since 1.0 */ function factorialPrint(int $n) { static $r; if ($n > 0) { $r[] = $n; return factorialPrint(--$n); } return 'RESULT: ' . implode(' x ', array_reverse($r)) . ' = ' . array_product($r); } /** * Simple recursive factorial function * * @param int $n * * @return int * * @since 1.0 */ function factorial(int $n): int { if ($n > 1) { return $n * factorial($n - 1); } else { return 1; } } /** * Show a range of factorials * * @param int $n * * @return mixed * * @since 1.0 */ function rangeFactorials(int $n) { // show the factorial echo "RESULT: factorial for $n = " . factorial($n) . '
'; // check if we are done if ($n > 0) { return rangeFactorials(--$n); } return true; } ?> Date & Factorials - Homework - PHP Scripts
'; // simple factorial option in PHP (does not work for zero) echo 'RESULT: factorial for 9 = ' . array_product(range(1, 9)) . '
'; // RESULT: factorial for 9 = 362880 // get the factorial of 5 echo factorialPrint(5) . '
'; // RESULT: 1 x 2 x 3 x 4 x 5 = 120 // get the factorial of 10 echo 'RESULT: factorial for 10 = ' . factorial(10) . '

'; // RESULT: factorial for 10 = 3628800 // Show all factorials for 0 - 10 rangeFactorials(10); // RESULT: factorial for 10 = 3628800 // RESULT: factorial for 9 = 362880 // RESULT: factorial for 8 = 40320 // RESULT: factorial for 7 = 5040 // RESULT: factorial for 6 = 720 // RESULT: factorial for 5 = 120 // RESULT: factorial for 4 = 24 // RESULT: factorial for 3 = 6 // RESULT: factorial for 2 = 2 // RESULT: factorial for 1 = 1 // RESULT: factorial for 0 = 1 ?>