Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define NUM 1000000000
- struct bignum {
- unsigned int ln;
- unsigned int ar[40];
- };
- void addtwo(unsigned int *, unsigned int *, unsigned int *);
- void addbignum (struct bignum *, struct bignum *);
- void fib2(unsigned int n)
- {
- struct bignum a1 = {0, {1}}, a2 = {0, {1}};
- struct bignum * ptr;
- unsigned int i;
- for(; n>1; n-=2)
- {
- addbignum(&a1,&a2);
- addbignum(&a2,&a1);
- }
- if (n==1) ptr = &a2;
- else ptr = &a1;
- if (ptr->ar[ptr->ln] == 0) ptr->ln--;
- printf("%u", ptr->ar[ptr->ln]);
- ptr->ln--;
- for(; ptr->ln!=-1; ptr->ln--)
- printf("%09u", ptr->ar[ptr->ln]);
- }
- // first = first+second
- // first < second
- void addbignum (struct bignum * a1, struct bignum * a2)
- {
- unsigned int a = 0;
- for (; a <= a1->ln; a++)
- {
- addtwo(&(a1->ar[a]), &(a2->ar[a]), &(a1->ar[a]));
- }
- if (a1->ln != a2->ln) printf("YOBA\n");
- if (a1->ar[a] != 0)
- {
- a1->ln++;
- a2->ln++;
- }
- }
- void addtwo(unsigned int *a, unsigned int *b, unsigned int *c)
- {
- unsigned int d=*a+*b;
- c[0] = d % NUM;
- c[1]+= d / NUM;
- }
- int main(void) {
- unsigned int a, n1=0, n2 = 1, ar[2];
- scanf("%u", &a);
- fib2(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement