-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.c
More file actions
87 lines (70 loc) · 1.63 KB
/
fib.c
File metadata and controls
87 lines (70 loc) · 1.63 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
// Calculate Fibonacci Numbers
// Public Domain
// https://creativecommons.org/publicdomain/zero/1.0/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <gmp.h>
#include <time.h>
#include <regex.h>
#include <immintrin.h>
long limit, i = 0;
int main(int argc, char *argv[])
{
// Create regex for url validation:
regex_t regex;
int reti;
char msgbuf[100];
reti = regcomp(®ex, "^[0-9]+$", 0);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
// Execute regex:
reti = regexec(®ex, argv[1], 0, NULL, 0);
if (!reti) {
puts("Match");
} else if (reti == REG_NOMATCH) {
puts("No match");
} else {
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
// Get User Input
if (argc != 2)
{
printf("Improper input. Exiting.\n");
return -1;
}
limit = strtol(argv[1], NULL, 10);
// Setup GMP
mpz_t a, b, c;
mpz_init_set_ui(a, 1);
mpz_init_set_ui(b, 0);
mpz_init(c);
// Start timing
clock_t start_time = clock();
for (i = 0; i < limit; i++)
{
// Perform the Fibonacci Calculation
mpz_add(c, a, b);
mpz_set(a, b);
mpz_set(b, c);
}
// End timing
clock_t end_time = clock();
// Print the results to stdout
printf("Fibonacci Number %ld: ", i);
mpz_out_str(stdout, 10, b);
printf("\n");
// Cleanup
mpz_clear(a);
mpz_clear(b);
mpz_clear(c);
// Print time taaken
double time_taken = ((double) end_time - start_time) / CLOCKS_PER_SEC;
printf("Calculation Time: %f seconds\n", time_taken);
return 0;
}