-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonaccisum.c
73 lines (57 loc) · 1.63 KB
/
fibonaccisum.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
//#include <stdlib.h>
unsigned long long fibonaccisum(int n) {
unsigned long long a = 0, b = 1, temp, sum = 1;
if (n <= 1) {
return n;
}
for (int i = 2; i <= n; ++i) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
//int/long overflow alternative
// void addArrays(int result[], int a[], int b[], int size) {
// int carry = 0;
// for (int i = size - 1; i >= 0; --i) {
// result[i] = a[i] + b[i] + carry;
// carry = result[i] / 10;
// result[i] %= 10;
// }
// }
// char* add2(int n) {
// const int MAX_DIGITS = 1000;
// int a[MAX_DIGITS] = {0};
// int b[MAX_DIGITS] = {0};
// int temp[MAX_DIGITS] = {0};
// a[MAX_DIGITS - 1] = 1;
// b[MAX_DIGITS - 1] = 1;
// for (int i = 3; i <= n; ++i) {
// addArrays(temp, a, b, MAX_DIGITS);
// for (int j = 0; j < MAX_DIGITS; ++j) {
// a[j] = b[j];
// b[j] = temp[j];
// }
// }
// // Find the first non-zero digit in the result
// int index = 0;
// while (temp[index] == 0 && index < MAX_DIGITS - 1) {
// index++;
// }
// // Calculate the length of the result string
// int length = MAX_DIGITS - index + 1; // +1 for null terminator
// // Allocate memory for the result string
// char* result = (char*)malloc(length);
// // Copy the result to the string
// for (int i = 0; i < length - 1; ++i) {
// result[i] = temp[index + i] + '0';
// }
// // Null-terminate the string
// result[length - 1] = '\0';
// return result;
// }
int main() {
return 0;
}