aboutsummaryrefslogtreecommitdiffstats
path: root/fib.c
diff options
context:
space:
mode:
authormjf <mjf@localhost.localdomain>2020-02-04 12:11:30 -0500
committermjf <mjf@localhost.localdomain>2020-02-04 12:11:30 -0500
commit6aa02212cd6dfbb492fa1f70b60a4fe3d48892e5 (patch)
tree1b655714c9709ee8fce333fcb6a6be3f5533b2d3 /fib.c
parent8e431d287c0e89041506da4c4da57e3e3d657d72 (diff)
downloadProject_Euler_Solutions-6aa02212cd6dfbb492fa1f70b60a4fe3d48892e5.tar.gz
cleanup and added more c examples:
Diffstat (limited to 'fib.c')
-rw-r--r--fib.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/fib.c b/fib.c
new file mode 100644
index 0000000..915fccd
--- /dev/null
+++ b/fib.c
@@ -0,0 +1,33 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+
+const long int MAX = 4 * pow(10, 6);
+int *fib;
+
+int main(){
+ // Intialize sequence with first two terms
+ fib = (int *) malloc (2 * sizeof(int));
+ fib[0] = 1;
+ fib[1] = 1;
+
+ // loop counter
+ int k = 1;
+ // sum of even terms
+ int sum = 0;
+ //next term in sequence
+ int next;
+ while(1){
+ next = fib[k] + fib[k - 1];
+ printf("%d\n", next);
+ if(next > MAX)
+ break;
+ if(next % 2 == 0)
+ sum += next;
+ k++;
+ fib = (int *) realloc (fib, (k + 1) * sizeof(int));
+ fib[k] = next;
+ }
+ printf("The sum of the even terms is %d\n", sum);
+ return 0;
+}