-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGMPFibonacci.c
More file actions
95 lines (73 loc) · 2.08 KB
/
Copy pathGMPFibonacci.c
File metadata and controls
95 lines (73 loc) · 2.08 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
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
//The fibonacci function using the normal fibonacci as other algorithms sacrifice either time or mrmory for the other.
void fibonacci(unsigned int n, mpz_t r){
if(n == 0){
mpz_set_ui(r, 0);
return;
}
else if(n == 1){
mpz_set_ui(r, 1);
}
else{
//Declaring and initializing the temporary variables for calculations
mpz_t n1;
mpz_t n2;
mpz_init_set_ui(n1, 0);
mpz_init_set_ui(n2, 1);
//Calculating the fibonacci numbers
for(unsigned int i = 2; i <= n; i++){
mpz_add(r, n1, n2);
mpz_set(n1, n2);
mpz_set(n2, r);
}
//Storing the final result
mpz_set(r, n2);
//Freeing the memory for the temporary variables.
mpz_clear(n1);
mpz_clear(n2);
}
}
int main(int argc, char* argv[]){
//Checking for command-line arguments
if(argc < 2){
printf("Use: %s <output_filename>\n", argv[0]);
return 1;
}
unsigned int n = 0;
mpz_t r;
mpz_init(r);
//Taking input and handling errors
do{
printf("\nInput a whole number: ");
scanf("%u", &n);
if (n < 0){
printf("\nInvalid input! Please enter a whole number.\n");
}
} while (n < 0);
//Calling the fibonacci function
fibonacci(n, r);
//Taking the output in a file
FILE *fp = fopen(argv[1], "w");
//Checking if the file opened
if(!fp){
printf("\nFailed to open the file.");
//Freeing the allocated memory
mpz_clear(r);
return 1;
}
else{
printf("\nOpened the file, sucessfully.\n");
}
fprintf(fp, "fib(%u) = ", n);
mpz_out_str(fp, 10, r);
fprintf(fp, "\n");
//Closing the file
fclose(fp);
//Printing the results
printf("\nCopied the fibonacci result to the file.\n");
//Freeing the allocated memory
mpz_clear(r);
return 0;
}