-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path495.cpp
More file actions
102 lines (74 loc) · 1.81 KB
/
495.cpp
File metadata and controls
102 lines (74 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#define MAXDIGITS 2000 /* maximum length bignum */
#define PLUS 1 /* positive sign bit */
#define MINUS -1 /* negative sign bit */
/* Fibonacci Freeze - 495 */
typedef struct {
char digits[MAXDIGITS]; /* represent the number */
int signbit; /* 1 if positive, -1 if negative */
int lastdigit; /* index of high-order digit */
} bignum;
print_bignum(bignum *n)
{
int i;
for (i=n->lastdigit; i>=0; i--)
printf("%c",'0'+ n->digits[i]);
printf("\n");
}
int_to_bignum(int s, bignum *n)
{
int i; /* counter */
int t; /* int to work with */
for (i=0; i<MAXDIGITS; i++) n->digits[i] = (char) 0;
n->lastdigit = -1;
t = abs(s);
while (t > 0) {
n->lastdigit ++;
n->digits[ n->lastdigit ] = (t % 10);
t = t / 10;
}
if (s == 0) n->lastdigit = 0;
}
initialize_bignum(bignum *n)
{
int_to_bignum(0,n);
}
int max(int a, int b)
{
if (a > b) return(a); else return(b);
}
add_bignum(bignum *a, bignum *b, bignum *c)
{
int carry; /* carry digit */
int i; /* counter */
initialize_bignum(c);
c->lastdigit = max(a->lastdigit,b->lastdigit)+1;
carry = 0;
for (i=0; i<=(c->lastdigit); i++) {
c->digits[i] = (char) (carry+a->digits[i]+b->digits[i]) % 10;
carry = (carry + a->digits[i] + b->digits[i]) / 10;
}
zero_justify(c);
}
zero_justify(bignum *n)
{
while ((n->lastdigit > 0) && (n->digits[ n->lastdigit ] == 0))
n->lastdigit --;
if ((n->lastdigit == 0) && (n->digits[0] == 0))
n->signbit = PLUS; /* hack to avoid -0 */
}
main()
{
int i,num;
bignum *n;
n=(bignum*)malloc(5000*sizeof(bignum));
int_to_bignum(0,&n[0]);
int_to_bignum(1,&n[1]);
for(i=2;i<5001;i++)
add_bignum(&n[i-2],&n[i-1],&n[i]);
while (scanf("%d",&num) != EOF)
{
printf("The Fibonacci number for %d is ",num);
print_bignum(&n[num]);
}
}