It is linear for the iterative version:
// O(n)
function factorial (n) {
let ret = 1;
for(let i = 2; i <= n; i++) {
ret = ret * i;
}
return ret;
}
and it appears to be linear for the recursive version as well:
function factorialR (n) {
if( n === 0 || n === 1 ) {
return 1;
} else {
return n * factorialR(n - 1);
}
}
Is it linear for the recursive version as well? Instead of a loop for each additional value it is just an additional function call.