parent
f2499624d7
commit
061108eb02
@ -0,0 +1,30 @@ |
||||
#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
|
||||
*/ |
||||
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; |
||||
} |
Loading…
Reference in new issue