Factorialalgorithm

Source

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: factorial formula

    1. normal solution - function factorNType1
    1. iterate solution - function factorNType2
    1. algorithmic solution - function factorNType3

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);
}