-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongstring_longmath.c
More file actions
44 lines (42 loc) · 1.11 KB
/
longstring_longmath.c
File metadata and controls
44 lines (42 loc) · 1.11 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int* stringToArray(char* str, int* length) {
int* array = malloc(10000 * sizeof(int));
char* token = strtok(str, " ");
int i = 0;
while (token != NULL) {
array[i++] = atoi(token);
token = strtok(NULL, " ");
}
*length = i;
return array;
}
long long fib(int n) {
if (n <= 1) {
return n;
}
return fib(n-1) + fib(n-2) + fib(n-2);
}
void complexMath(int* array, int length) {
for (int i = 0; i < length; i++) {
array[i] = fib(array[i]);
}
}
int main() {
char input[] = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";
int length;
int* numbers = stringToArray(input, &length);
clock_t start = clock();
complexMath(numbers, length);
clock_t end = clock();
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("Result after complex math:\n");
for (int i = 0; i < length; i++) {
printf("%lld ", (long long)numbers[i]);
printf("\nTime taken for complex math: %f seconds\n", time_spent);
free(numbers);
return 0;
}
}