Help Calculating Total Numbers Through a Loop

Hi,

I'm doing a project for my class and I have almost everything done, but I'm just a little stuck.

Write a program that has a fibonacci number calculating function. The function will take an integer as a parameter which is the fibonacci number to calculate, and return the fibonacci number. Then use this function to calculate the sum of every fibonacci number that is divisible by 3 or 5,(hint - use modulus division). Do this for all fibonacci numbers less than 4,000,000.

Here's my code, I have everything done, but I can't figure out how to add numbers divisible by 3 and 5 to an overall total then display the total amount of all the numbers that were divisible by 3 and 5.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main(){
	int input(31), current(0), pervious(1), total(1), new_total;

	for(int i = 0; i <= input; i++){

    //Calculate the fibonacci numbers and display only the number divisble by 3 and 5
	total = current + pervious;
	current = pervious;
	pervious = total;

    if ( total % 3 == 0 )
    {
        cout << total << endl;
	}

    if ( total % 5 == 0 )
    {
        cout << total << endl;
	}

//Add the sum of divisble numbers then display the final number once 2170389 is reached
while (total <= 2170389)
    {
        new_total = total + total;
        cout << new_total << endl;

    }

  }
    return 0;
}
Last edited on
i dunno bout you but for me, there are 2 questions in this question alone.

one: Write a program that has a fibonacci number calculating function. The function will take an integer as a parameter which is the fibonacci number to calculate, and return the fibonacci number.

two: from the previous function, improve the function to calculate the sum of every fibonacci number that is divisible by 3 or 5,(hint - use modulus division). Do this for all fibonacci numbers less than 4,000,000.


i think you are trying to solve the second one, so yeah. logically, you calculate the fibonacci number, then add them up. the sum of them must not exceed 4 million.

- use the formula of Fn = F(n-1) + F(n-2) to find the fibonacci number, where n starts from 0.

- if Fn is divisible by 3 and 5, then Fn is then added into a variable "sum_of_fibonacci_num"

- all this in a while loop and the condition will be "while (sum_of_fibonacci_num<4mil)"

hope this answers your question
Thanks for the reply.

Yes, I already had this but I still can't get it to work.

The highest number I can go without reaching 4,000,000 is 2,170,389.

So how do I calculate it to add the divisible numbers to a total? I already have the setup you said above and it's not working.

1
2
3
4
5
6
while (total <= 2170389)
    {
        new_total = total + total;
        cout << new_total << endl;

    }


Is there something I'm doing wrong? I've been working on this for the last few days and I'm running out of time. I must just be overlooking something...
Last edited on
edit:
1. problem lies on the fact that you used LESS THAN OR EQUALS TO. remember, that even if the sum reaches 2,170,389, because it is EQUALS to the condition, the condition is true, and process runs again and adds up again to exceed 4 million. get it? try removing the equals sign, and i think it should work :/

2. your solution calculates the sum of sum of fibonacci numbers, divisible by 3 and 5. you check if the sum is divisible by 3 and 5, not the fibonacci number.

the question needs sum of, fibonacci numbers divisible by 3 and 5.
--

i have another way to deal with this. well, bear with me as i have little to no knowledge of fibonacci numbers XD just wiki-ed it recently :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//initialize n as 0
n=0;
do {
  //find fibonacci number, result is stored into F_n
  F_n=...;

  //if divisible by 3 or 5
  if (F_n%3==0 || F_n%5==0)
  {
    //we first store the previous sum into somewhere
    previous_sum = sum_of_fibonacci_number;

    //then we add the NEW fibonacci number into the sum
    sum_of_fibonacci_number = F_n + sum_of_fibonacci_number;
  }
  //update the n value so they calculate the next fibnacci number
  n++;

//and all this while the sum is less than 4 mil
} while (sum_of_fibonacci_number<4000000);

//then your previous sum should be LESS THAN 4 mil
cout << previous_sum << endl;

notice that we check sum_of_fibonacci_number and result is previous_sum. to "answer" the question, we must have the condition as 4 mil.

Do this for all fibonacci numbers less than 4,000,000.

we cannot just assume that we know the final value before 4 mil. this will make your code pretty much "just for this case, i can solve it", and not flexible if say, make the limit 400? 8 mil?

its a little bit complex, i have to admit. so please do ask if you still do not understand. hope this helps :)
Last edited on
Topic archived. No new replies allowed.