Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

Infer ethical and legal concerns in HR data collection

Answered: 1 week ago