Advertisement
Miquel_Fuster

Fibonacci sequence with generating function

Oct 4th, 2023
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. unsigned fibonacci(bool reset) {
  5.     static unsigned n1 = 0;
  6.     static unsigned n2 = 1;
  7.  
  8.     if(reset) {
  9.         n1 = 0;
  10.         n2 = 1;
  11.     }
  12.  
  13.     unsigned retval = n1;
  14.  
  15.     n2 += n1;
  16.     n1 = n2 - n1;
  17.  
  18.     return retval;
  19. }
  20.  
  21. int main() {
  22.     for(size_t i = 0; i < 20; ++i) {
  23.         if(i == 10) printf("\n-- reiniciando --\n");
  24.         printf("%u\n", fibonacci(i == 10));
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement