Advertisement
XeBuZer0

Fibonacci's secuence in C (iterative)

Dec 21st, 2019 (edited)
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** //
  2. // ** // ** // ** // Código fuente de programa que calcula el n-ésimo término // ** // ** // ** //
  3. // ** // ** // ** // ** / de la secuencia de Fibonacci (iterativo) / ** // ** // ** // ** // ** //
  4. // ** // ** // ** // * Licenciado bajo GNU General Public License (GPL) 3.0 * // ** // ** // ** //
  5. // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** //
  6. /* ** // ** // ** // ** // ** //  * F v q _ U k r a N a z i s ! * // ** // ** // ** // ** // ** */
  7. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  8.  
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <sys/time.h>
  12.  
  13. long FiboIter(long n);
  14.  
  15. int main(void){
  16.     register long x=0, res=0;
  17.     register long long pre_time=0;
  18.     struct timeval tv;
  19.     for (x=1;x<=35;x++) {
  20.         gettimeofday(&tv, NULL);      
  21.         pre_time = tv.tv_sec*1000000+tv.tv_usec;
  22.         res = FiboIter(x);
  23.         gettimeofday(&tv, NULL);
  24.         printf("Element: %ld Result: %ld Time: %lld\n", x, res, tv.tv_sec*1000000+tv.tv_usec - pre_time);
  25.     }
  26.     return 0;
  27. }
  28.  
  29. long FiboIter(long n){
  30.     long a=0, b=0, S=0, cont=1;
  31.     if (0<n) for (a=0,b=1;cont<n;cont++){
  32.         S = a+b;
  33.         a = b;
  34.         b = S;
  35.     }
  36.     return S;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement