Categories

Math2

Factorial

Calculates the factorial of a number.

Contributed by @itsbrunodev

rust
fn factorial(n: u32) -> u32 {
    match n {
        0 | 1 => 1,
        _ => n * factorial(n - 1),
    }
}
rust
factorial(5); // 120
GitHubEdit on GitHub