Problem 2.
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
f2499624d7
commit
061108eb02
30
archives/2/src/main.c
Normal file
30
archives/2/src/main.c
Normal file
@ -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;
|
||||||
|
}
|
16
src/main.c
16
src/main.c
@ -10,5 +10,21 @@ By considering the terms in the Fibonacci sequence whose values do not exceed fo
|
|||||||
https://projecteuler.net/problem=2
|
https://projecteuler.net/problem=2
|
||||||
*/
|
*/
|
||||||
int main(int argc,char**argv) {
|
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;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user