Find n th fibonacci iteratively - illustrated walkthrough

329 views

Published on

A step-by-step illustration of Find n-th Fibonacci function to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation.

https://github.com/yoshiwatanabe/Algorithms/blob/master/Finding/Fibonacci.cs

Published in: Technology, Travel, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
329
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Find n th fibonacci iteratively - illustrated walkthrough

  1. 1. Find n-th Fibonacci iteratively Illustrated walk through
  2. 2. public static long FindNthFibonacciIterative(int n) { if (n == 0) return 0; if (n == 1) return 1; long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; }
  3. 3. Let’s just get over with two special cases when n == 0 [0] [1] [2] [3] [4] [5] [6] 0 1 ? ? ? ? ? if (n == 0) return 0; if (n == 1) return 1; Always return 0 when n == 1 [0] [1] [2] [3] [4] [5] [6] 0 1 ? ? ? ? ? if (n == 0) return 0; if (n == 1) return 1; Always return 1
  4. 4. [0] [1] [2] [3] [4] [5] [6] 0 1 ? ? ? ? ? fibNMinusTwo fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; fibN
  5. 5. when n == 6 0 fibNMinusTwo 1 ? ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  6. 6. 0 fibNMinusTwo 1 0 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  7. 7. n == 6 i=2 0 fibNMinusTwo 1 0 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  8. 8. n == 6 i=2 0 fibNMinusTwo 1 0 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 2 <= 6 is true
  9. 9. n == 6 i=2 0 + fibNMinusTwo 1 1 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 2nd Fibonacchi is 1
  10. 10. n == 6 i=2 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  11. 11. n == 6 i=2 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  12. 12. n == 6 i=3 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  13. 13. n == 6 i=3 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 3 <= 6 is true
  14. 14. 3ed fib. n == 6 i=3 1 + fibNMinusTwo 1 2 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 3nd Fibonacchi is 2
  15. 15. n == 6 i=3 1 fibNMinusTwo 1 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  16. 16. n == 6 i=3 1 fibNMinusTwo 2 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  17. 17. n == 6 i=4 1 fibNMinusTwo 2 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  18. 18. n == 6 i=4 1 fibNMinusTwo 2 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 4 <= 6 is true
  19. 19. n == 6 i=4 1 + fibNMinusTwo 2 3 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 4-th Fibonacchi is 3
  20. 20. n == 6 i=4 2 fibNMinusTwo 2 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  21. 21. n == 6 i=4 2 fibNMinusTwo 3 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  22. 22. n == 6 i=5 2 fibNMinusTwo 3 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  23. 23. n == 6 i=5 2 fibNMinusTwo 3 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 5 <= 6 is true
  24. 24. n == 6 i=5 2 + fibNMinusTwo 3 5 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 5-th Fibonacchi is 5
  25. 25. n == 6 i=5 3 fibNMinusTwo 3 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  26. 26. n == 6 i=5 3 fibNMinusTwo 5 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  27. 27. n == 6 i=6 3 fibNMinusTwo 5 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  28. 28. n == 6 i=6 3 fibNMinusTwo 5 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 6 <= 6 is true
  29. 29. n == 6 i=6 3 + fibNMinusTwo 5 8 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 6-th Fibonacchi is 8
  30. 30. n == 6 i=6 5 fibNMinusTwo 5 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  31. 31. n == 6 i=6 5 fibNMinusTwo 8 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  32. 32. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  33. 33. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 7 <= 6 is false
  34. 34. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; Exit the for loop
  35. 35. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne ? ? fibN Return fibN = 8 long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; 6-th Fibonacchi is 8 for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN;

×