Even Fibonacci numbers
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.
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.
Solution: Complete
int num = 33;
int numTerms = num/3;
int fib[] = new int[num+1];
int evenFib[] = new int[numTerms];
int sumFib = 0;
void setup(){
fib[0] = 0;
fib[1] = 1;
for(int x = 2; x <=num; x++){
fib[x] = fib[x-1] + fib[x-2];
}
for(int x2 = 0; x2<=num; x2 += 3){
sumFib += fib[x2];
}
println(sumFib);
}
int numTerms = num/3;
int fib[] = new int[num+1];
int evenFib[] = new int[numTerms];
int sumFib = 0;
void setup(){
fib[0] = 0;
fib[1] = 1;
for(int x = 2; x <=num; x++){
fib[x] = fib[x-1] + fib[x-2];
}
for(int x2 = 0; x2<=num; x2 += 3){
sumFib += fib[x2];
}
println(sumFib);
}