-
Notifications
You must be signed in to change notification settings - Fork 0
/
025.c
57 lines (44 loc) · 1.2 KB
/
025.c
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
// What is the first term in the Fibonacci sequence to contain 1000 digits?
#include <stdio.h>
int main () {
void addArrays(int addThis[], int toThis[]);
int i = 1;
int stop_at_digits = 1000;
int stop_at_index = 1000 - stop_at_digits;
int fib1[1000] = {0};
int fib2[1000] = {0};
fib1[999] = 1;
fib2[999] = 1;
while (i < 10000 /* arbitrary upper bound */) {
addArrays(fib1, fib2);
addArrays(fib2, fib1);
i++;
if (fib1[stop_at_index] != 0 || fib2[stop_at_index] != 0) {
break;
}
}
printf("Fib1 = F%i, Fib2 = F%i\n", i * 2, (i * 2) - 1);
printf("Target digits:\n");
printf("Fib1: %i, Fib2: %i\n", fib1[stop_at_index], fib2[stop_at_index]);
return 0;
}
// Add two long number-like arrays
void addArrays(int addThis[], int toThis[]) {
void incrementDigit(int *arr, const int i);
for (int i = 0; i < 1000; i++) {
toThis[i] += addThis[i];
if (toThis[i] > 9) { // carry over
toThis[i] -= 10;
incrementDigit(toThis, i - 1);
}
}
}
// Increment digit at index i
void incrementDigit(int *arr, const int i) {
if (*(arr + i) == 9) {
incrementDigit(arr, i - 1);
*(arr + i) = 0;
} else {
*(arr + i) += 1;
}
}