Advertisement
Guest User

Untitled

a guest
Jan 21st, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. int fib(int n)
  2. {
  3.     int num1 = 0, num2 = 1, cur, cur_index = 1;
  4.     if (n < 2) return n;
  5.     while (cur_index < n)      //כל עוד האינדקס הנוכחי קטן מהאינדקס המבוקש
  6.     {
  7.         cur = num1+num2;  //סוכמים את שני האיברים הראשונים בסדרת פיבונצי
  8.         num1 = num2;      //משימים את האיבר "האחרון" לבא אחריו
  9.         num2 = cur;       //משימים את האיבר האחרון לסכום שקיבלנו באיטרציה הקודמת
  10.         cur_index++;      //מקדמים את האינדקס לאינדקס הבא בתור, וחוזר חלילה עד שמגיעים לאינדקס שקיבלנו כפרמטר
  11.     }
  12.  
  13.     return cur;
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement