diff options
Diffstat (limited to '02-Even-Fibonacci-numbers')
-rwxr-xr-x | 02-Even-Fibonacci-numbers/fib | bin | 0 -> 16776 bytes | |||
-rw-r--r-- | 02-Even-Fibonacci-numbers/fib.c | 33 | ||||
-rw-r--r-- | 02-Even-Fibonacci-numbers/fib.py | 34 |
3 files changed, 67 insertions, 0 deletions
diff --git a/02-Even-Fibonacci-numbers/fib b/02-Even-Fibonacci-numbers/fib Binary files differnew file mode 100755 index 0000000..e9b1b3b --- /dev/null +++ b/02-Even-Fibonacci-numbers/fib diff --git a/02-Even-Fibonacci-numbers/fib.c b/02-Even-Fibonacci-numbers/fib.c new file mode 100644 index 0000000..915fccd --- /dev/null +++ b/02-Even-Fibonacci-numbers/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; +} diff --git a/02-Even-Fibonacci-numbers/fib.py b/02-Even-Fibonacci-numbers/fib.py new file mode 100644 index 0000000..c2f2691 --- /dev/null +++ b/02-Even-Fibonacci-numbers/fib.py @@ -0,0 +1,34 @@ +import PIL +import math +MAX = 4*10**6 + +# Problem 2 Even Fibonnacci numbers +fib = [1, 1] + +k = 1 +while (True): + n = fib[k] + fib[k-1] + if(n > MAX): + break + else: + fib.append(n) + k += 1 + +c = 0 +for i in fib: + num = str(i) + print(num + " ", end='') + c += 1 + if(c % 10 == 0): + print() + +print() + +print("The sum is: " + str(sum(fib))) + +s = 0 +for i in fib: + if (i % 2 == 0): + s += i + +print("The sum of the even terms is: " + str(s)) |