seergiomv

Práctica algoritmos

Sep 23rd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <sys/time.h>
  4.  
  5. double microsegundos(){
  6.     struct timeval t;
  7.     if (gettimeofday(&t, NULL) < 0){
  8.         return 0.0;
  9.     } return (t.tv_usec + t.tv_sec * 1000000.0);
  10. }
  11.  
  12. int fib1(int n){
  13.     if (n < 2){
  14.         return n;
  15.     }
  16.     return fib1(n - 1) + fib1(n - 2);
  17. }
  18.  
  19. int fib2(int n){
  20.     int i, j;
  21.     i = 1; j = 0;
  22.     for (int k = 1; k < n; k++) {
  23.         j = i + j;
  24.         i = j - i;
  25.     }
  26.     return j;
  27. }
  28.  
  29. int fib3(int n){
  30.     int i, j, k, h, t;
  31.     i = 1; j = 0; k = 0; h = 1;
  32.     while (n > 0) {
  33.         if ((n % 2) != 0){
  34.             t = j * h;
  35.             j = (i * h) + (j * k) + t;
  36.             i = (i * k) * t;
  37.         }
  38.         t = h * h;
  39.         h = (2 * k * h) + t;
  40.         k = (k * k) + t;
  41.         n = n / 2;
  42.     }
  43.     return j;
  44. }
  45.  
  46. int main(){
  47.     int n;
  48.     double t1, t2, t, x, y, z;
  49.     n = 1000000;
  50.     t1 = microsegundos();
  51.     fib3(n);
  52.     t2 = microsegundos();
  53.     t = t2 - t1;
  54.     x = t / sqrt(log(n));
  55.     y = t / log(n);
  56.     z = t / pow(n, 0.5);
  57.     printf("%12d%15.3f%15.6f%15.6f%15.6f\n",n, t, x, y, z);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment