ProjectEuler/src/main.c

30 lines
842 B
C
Raw Normal View History

2022-07-14 07:56:56 -05:00
#include <stdio.h>
/*
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
https://projecteuler.net/problem=2
*/
2022-07-14 07:56:56 -05:00
int main(int argc,char**argv) {
int fibStorage[1000];
int val=0;
int counter=0;
int sum=0;
while (val<4000000) {
if (counter>2) {
val=fibStorage[counter++]=fibStorage[counter-2]+fibStorage[counter-1];
} else {
val=fibStorage[counter++]=counter;
}
printf("Fib %d is %d\n",counter,val);
if ((val&1)==0) {
sum+=val;
}
}
printf("Sum: %d",sum);
return 0;
2022-07-14 07:56:56 -05:00
}