You are here:

C++/Fibonacci Sequence


QUESTION: I am trying to do a fibonacci sequence (to help me learn). It is to figure out the sum of even valued terms in the sequence. What I am confused about is:

1) I see people using things such as i%2==0. From everything I read % is either for pointer refrences or for division with remainders. With this being the case could you explain why it is used in this method?

2) If it can't be used in this method. How would I go about making the numbers = 0. Such as the sequence goes on it would get to say 2000000. That / by 2 is 1000000. Would I have to up the /2 to make it work properly?

Thank you.

ANSWER: Hello Will.

The % operator is used for modulo division. It returns the remainder from a division operation. Your assignment is to find the sum of the even numbers in a sequence. If N is a number in the sequence, then N is even when dividing it by 2 produces no remainder. The statement N%2==0 is producing a remainder from the division of N by 2, and then comparing that remainder against 0.

The % operator has nothing to do with pointers. You may be confusing it with &, which is used to find the address of some variable.

I don't really understand your second question, but I think it is coming from some misconception you have. There is no need to divide by 2.

The Fibonacci sequence goes on forever, so you must have some instruction in your assignment about how many terms to generate. The first two terms are 1, 1. From there on, the next term is the addition of the previous two terms. As I understand your assignment, you must generate all the terms, but for each term generated, you must decide if it is even or odd, and add only the even terms into a separate sum.

To generate the terms you will have to keep track of the last two terms generated. Lets call them L1 and L2. Lets call the next term N. Then N=L1+L2. Once you have N, you must get ready for the next iteration by putting L1 into L2, and putting N into L1. Does that make sense ?

I suggest you start by writing code to generate a certain number of Fibonacci numbers, and print them out. Once you have that working, then add the idea of summing the even terms as they are generated.

I hope that helps you get started.

Best regards

---------- FOLLOW-UP ----------

QUESTION: Thank you for the quick response. My second question was a bit confusing but what I mean is. If you have n%2==0. say n equals 2000000. So it would be 2000000%2==0. Which in turn is 2000000/2 remainder = 0. Yet it equals to 1000000. So how would it equal to 0? Or does the mobolus symbol just look for a remainder and that is it? Like a boolan statement. If it has a remainder then it is automatically odd/false, and if it has no remainder then it is automatically even/true?

Hello Will.

The modulus is separate from division. 2000000%2 is not division so it is a misconception to say that it is

"in turn 2000000/2 remainder = 0".

The modulo operator % returns only the remainder that would be left if the division had been done. Furthermore, it returns a number, not a Boolean value. In C and C++, a number can be interpreted as a Boolean value with 0 being false and non-zero being true, so in that sense, you can say that modulo 2 returns a true/false about a number being odd/even, but it is important to understand that the point of the modulo operator is not to return true/false about a number being odd or even. In general, you will want to compare the modulo result against some number. In your case you want to compare the modulo result (the remainder) against 0, and yes, you are correct, if the remainder is 0, the number must be even. You know that from basic arithmetic.

I hope that helps you out.

Sorry if I seem picky here about the details, but I think it is important to have the deeper understanding about the meaning of things to be good at programming.

Best regards


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2017 All rights reserved.