Do both these factorial functions run in O(n)?

redlucasxp

New Member
The recursive function defined as so:\[code\]function factrec($x) { if($x <= 1) { return $x; } else { return $x * factrec($x - 1); }}\[/code\]And iterative here:\[code\]function factiter($x) { $y = $x; while($y > 1) { $x *= ($y - 1); $y--; } return $x;}\[/code\]I had read that on the recursive function the body is O(1) and the recursive calls O(n-1) making it O(n), but for the iterative is it O(n) as well?
 
Back
Top