ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

PHP Factorial Calculation

PHP

Goal -- WPM

Ready
Exercise Algorithm Area
1<?php
2
3/**
4* Calculates the factorial of a non-negative integer using recursion.
5*
6* The factorial of a non-negative integer n, denoted by n!, is the product
7* of all positive integers less than or equal to n.
8* 0! is defined as 1.
9*
10* @param int $n The non-negative integer for which to calculate the factorial.
11* @return int|string The factorial of n, or an error message for invalid input.
12*/
13function calculateFactorial(int $n):
14int|string
15{
16// Edge case: Handle negative input, as factorial is not defined for negative numbers.
17if ($n < 0) {
18return "Error: Factorial is not defined for negative numbers.";
19}
20
21// Base case: Factorial of 0 is 1.
22if ($n === 0) {
23return 1;
24}
25
26// Recursive step: n! = n * (n-1)!
27// We use a helper function to avoid re-checking the negative input condition in every recursive call.
28return $n * calculateFactorialHelper($n - 1);
29}
30
31/**
32* Helper function for recursive factorial calculation.
33* Assumes input is non-negative.
34*
35* @param int $n The current number in the factorial calculation.
36* @return int The result of the factorial calculation.
37*/
38function calculateFactorialHelper(int $n):
39int
40{
41// Base case for the helper: when n reaches 0, return 1.
42if ($n === 0) {
43return 1;
44}
45
46// Recursive step: n * (n-1)!
47return $n * calculateFactorialHelper($n - 1);
48}
49
50?>
Algorithm description viewbox

PHP Factorial Calculation

Algorithm description:

This PHP code calculates the factorial of a non-negative integer using recursion. The factorial of a number `n` is the product of all positive integers less than or equal to `n`. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. This function also handles the edge case of negative input by returning an error message.

Algorithm explanation:

The `calculateFactorial` function computes n! recursively. It first checks for invalid input (negative numbers) and returns an error. The base case for the recursion is when `n` is 0, in which case it returns 1 (since 0! = 1). For any `n` greater than 0, it recursively calls itself with `n-1` and multiplies the result by `n`. The `calculateFactorialHelper` function is used to streamline the recursive calls, ensuring the negative input check is only performed once. The time complexity is O(n) because each number from n down to 1 is processed once. The space complexity is O(n) due to the recursion depth, which can lead to stack overflow for very large `n`. The invariant maintained is that `calculateFactorialHelper(k)` correctly computes k! for all `k` from 0 to `n`.

Pseudocode:

function calculateFactorial(n):
  if n < 0:
    return "Error: Negative input"
  if n == 0:
    return 1
  return n * calculateFactorialHelper(n - 1)

function calculateFactorialHelper(n):
  if n == 0:
    return 1
  return n * calculateFactorialHelper(n - 1)