Question
Homework 04 - Even Fibonacci Numbers The following problem is taken from Project Euler. Each new term in the Fibonacci sequence is generated by adding
Homework 04 - Even Fibonacci Numbers
The following problem is taken from Project Euler. 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.
Tips:
1) To test and make sure your program works, why not start with a lower number. If you were to add up all even numbers whose value doesnt exceed 100, youd get 44. If you add all even numbers whose value doesnt exceed 200, youd get 188.
Bonus Objectives:
Instead of a maximum value of four million, how about 3 billion?
Tips:
1) The values an int can store is typically between -32,767 and 32,767.
2) The values a long int can store is typically between -2,147,483,647 and 2,147,483,647.
3) Look up unsigned int, By eliminating negative values we can almost double our range. Use
this to solve 3 billion (thats 3 followed by 9 zeros).
#include
using namespace std;
int main(){ int sum = 0; int a=0; int b=1; int c=0; for (int i=0; i<4000000;i++){ c=a+b; a=b=c; if( i%2 == 0){ sum = sum+c; } } cout << " Answer: " << sum << endl; return 0; }
Can you please correct my code? I don't know why am doing this wrong, and give short explanation. Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started