Advertisement
Guest User

fib4

a guest
Dec 20th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  1. int Fib::find_fib(int n)
  2. {
  3.   int v[3];
  4.   int i;
  5.  
  6.   if (n == 0 || n == 1) return 1;
  7.  
  8.   v[0] = 1;
  9.   v[1] = 1;
  10.   for (i = 2; i <= n; i++) {
  11.     v[2] = v[0] + v[1];
  12.     v[0] = v[1];
  13.     v[1] = v[2];
  14.   }
  15.  
  16.   return v[2];
  17. }
  18.  
  19. //instead of using a cache that is n items in size, let's just use three. This saves hella memory with large inputs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement