In this challenge, you will implement a function to calculate the factorial of a given non-negative integer. The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. For example, the factorial of 5 is 5! = 5 * 4 * 3 * 2 * 1 = 120.
Factorials grow very quickly, so it is important to handle large numbers appropriately. Rust's standard library provides the u128 type, which can handle very large integers. Your task is to implement a function that calculates the factorial of a given u32 number and returns the result as a u128.
Your task
Implement a function factorial(n: u32) -> u128 that calculates the factorial of the given non-negative integer n. Use early returns to handle the base case when n is 0, since 0! is defined as 1. For other values of n, use a loop to calculate the factorial.
Requirements
- The function should take a single argument
nof typeu32and return au128. - Use early returns to handle the case when
nis0. - For other values of
n, use a loop to calculate the factorial.
Example
let result = factorial(5);assert_eq!(result, 120);let result = factorial(10);assert_eq!(result, 3628800);let result = factorial(0);assert_eq!(result, 1);
Hints
- Remember that Rust has a
u128type which can hold very large numbers. This will be useful for calculating large factorials. - Use the
returnstatement to handle the base case of0!. - A
forloop orwhileloop can be used to multiply the numbers from1ton. - Consider using an iterative approach rather than a recursive one to avoid potential stack overflow issues for large
n.
pub fn factorial(n: u32) -> u128 {// Implement your code here}