Factorial iteration java -
pocould explain piece of code me?? power function using iteration
public static int iterate(int a, int n) { int ; int result = 1 ; for(i = 0 ; < n ; i++){ result = result*a ; } return result ; }
it helps know definition of factorial:
0! = 1 1! = 1 2! = 2*1 = 2 3! = 3*2*1 = 6 4! = 4*3*2*1 = 24 n! = n*(n-1)*(n-2)*...*2*1
see pattern?
- start result = 1
- loop , multiply index
- return result
what posted looks more a^n = a*a*a...*a
me, not factorial.