Challenge: Compute Factorial of a Number
This lesson brings you a challenge to solve.
We'll cover the following
Problem statement
Write a function that returns the factorial (!) of any n
number.
The factorial of a number is defined as:
For example:
Input
A number of type uint64, n
Output
A number of type uint64 (because the number increases drastically due to multiplication)
Results will be correct until only 21!
. After that, the overflow-error will occur.
Remark: When using type int the calculation is only correct up until
12!
. This is, of course, because an int can only contain integers that fit in 32 bits. And, Go doesn’t usually warn against this overflow-error!
Sample input
10
Sample output
3628800
Try to implement the function below. Feel free to view the solution, after giving some shots. Good Luck!
Get hands-on with 1400+ tech skills courses.