Hi Lee Davis,
At first look the non recursive function looked right to me, but then I was intrigued why you would have initialize and array with [0,1,1] elements. I understand that this is just an example of how we can optimize the overall time recursive and non recursive function take, but the example should make sense and should be testable. Any way I am sure you had a good reason for that, the main point here is for anyone else reading and comparing the performance of recursive and non recursive fibonacci : note – the $res array should contain [0,1,2] the reason fibonacci sums the previous and previous of the previous current number in your result it will not sum the last and previous elements. eg fibonacci(9) with your example will return 21 and the correct result is 34. This is how your $res[$n-1]looks:
[code]
array (size=9)
0 => int 0
1 => int 1
2 => int 1
3 => int 2
4 => int 3
5 => int 5
6 => int 8
7 => int 13
8 => int 21
[code/]
This is how it should be:
[code]
array (size=9)
0 => int 0
1 => int 1
2 => int 2
3 => int 3
4 => int 5
5 => int 8
6 => int 13
7 => int 21
8 => int 34
[code/]