Advertisement
Guest User

Untitled

a guest
May 13th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #define NUM 1000000000
  3.  
  4. struct bignum {
  5.    unsigned int ln;
  6.    unsigned int ar[40];
  7. };
  8. void addtwo(unsigned int *, unsigned int *, unsigned int *);
  9.  
  10. void addbignum (struct bignum *, struct bignum *);
  11.  
  12. void fib2(unsigned int n)
  13. {
  14.   struct bignum a1 = {0, {1}}, a2  = {0, {1}};
  15.   struct bignum * ptr;
  16.   unsigned int i;
  17.   for(; n>1; n-=2)
  18.   {
  19.     addbignum(&a1,&a2);
  20.     addbignum(&a2,&a1);
  21.   }
  22.   if (n==1) ptr = &a2;
  23.   else ptr = &a1;
  24.   if (ptr->ar[ptr->ln] == 0) ptr->ln--;
  25.   printf("%u", ptr->ar[ptr->ln]);
  26.   ptr->ln--;
  27.   for(; ptr->ln!=-1; ptr->ln--)
  28.     printf("%09u", ptr->ar[ptr->ln]);
  29. }
  30.  
  31. // first = first+second
  32. // first < second
  33. void addbignum (struct bignum * a1, struct bignum * a2)
  34. {
  35.   unsigned int a = 0;
  36.   for (; a <= a1->ln; a++)
  37.   {
  38.     addtwo(&(a1->ar[a]), &(a2->ar[a]), &(a1->ar[a]));
  39.   }
  40.   if (a1->ln != a2->ln) printf("YOBA\n");
  41.   if (a1->ar[a] != 0)
  42.   {
  43.     a1->ln++;
  44.     a2->ln++;
  45.   }
  46. }
  47.  
  48. void addtwo(unsigned int *a, unsigned int *b, unsigned int *c)
  49. {
  50.   unsigned int d=*a+*b;
  51.   c[0] = d % NUM;
  52.   c[1]+= d / NUM;
  53. }
  54.  
  55. int main(void) {
  56.   unsigned int a, n1=0, n2 = 1, ar[2];
  57.   scanf("%u", &a);
  58.   fib2(a);
  59.   return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement