Factorialalgorithm
FACTORIAL
In mathematics, the factorial of a non-negative integer n, denoted by ! n!, is the product of all positive integers less than or equal to n. The factorial of n also equals the product of n with the next smaller factorial:
-
- normal solution -
function factorNType1
- normal solution -
-
- iterate solution -
function factorNType2
- iterate solution -
-
- algorithmic solution -
function factorNType3
- algorithmic solution -
example of Number!
- 4! = 1x2x3x4 = 24
- 5! = 5x4x3x2 = 120
! is the symbol of factorial, place after the num => 5!
Review
- SmartContract
function factorNType3(uint num) public pure returns (uint) {
if(num == 0 || num == 1) {
return 1;
}
return num * factorNType3(num-1);
}