Please help me with Fibonacci #

Wow... ive been working on this for three hours today. I admit, my math is lacking... but I didn't think it was this bad. Please excuse all my extra variables.

Someone please help me with this... Im pulling my hair out at this point. lol
And i've reviewed the tutorials on fibonacci numbers. I really need to hear the explanation in terms of this question:

"It takes an organism two time periods to mature to reproduction age, and then the organism reproduces once per time period...

Assume that this green crud pile organism grows at this rate. 1 time period = 5 days. Hence, if a green crud population starts out as 10 pounds of crud, then in 5 days there will still be 10 pounds, 10 days: 20 pounds, 15 days: 30 pounds, 20 days: 50 pounds. Write a program that takes both the initial size of the population (in lbs) and the # of days it will mature. Assume that the population size is the same for 4 days, then doubles on the 5th."

Thank you. I look forward to fresh info... Im going to go have a drink. lol

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
//Global constants
//Function Prototypes
//Execution
int main(int argc, char** argv) {
    //Variables
    float isize, time, growth, growth2, growth3, rate, fib, n;
    //Input
    cout<<"Input the initial size of green crud in pounds"<<endl;
    cin>>isize;
    growth=isize;
    cout<<"Input how many days you would like to have the population grow"<<endl;
    cin>>time;
    rate=time/5;
    if ((1==rate)||(2==rate)) {
        cout<<"Population size is"<<isize<<endl;
    }
    else {
        while (rate>=3) {
            growth2=growth++;
            growth3=growth2++;
            growth=growth2+growth3;
            rate--;
            }
        cout<<"Crud pile is "<<growth<<endl;
    }
    return 0;
}
Fibonacci sequence:
fn = fn-2 + fn-1


Though you could probably do it without this.
doubles on the 5th
so every 5 days the population is doubled.

1
2
3
4
5
6
for(int i = 1; i <= days; ++i)
{
    if(i % 5 == 0)
        population <<= 1; //or *= 2 (double it)
    std::cout << "Day " << day << " population: " << population;
}

I'm pretty sure "doubles on the fifth" is for setting up initial conditions for the Fibonacci sequence. That is, P(5) = 2*P(4), not doubles every 5th day. His example of the population growth shows a Fibbonacci sequence: 10, 20, 30, 50 (and then 80 presumably).

As Giblit mentioned, the Fibonacci sequence follows:
a(n) = a(n-1) + a(n-2)

Where usually a(0) = a(1) = 1 (unless you want to count the 0 as part of the sequence).
In your case a(0) = 1 and a(1) = 2, so you're merely starting one term ahead.
1
2
3
4
5
6
7
8
9
10
11
12
13
//Untested code
unsigned int Fibonacci(std::size_t n, unsigned int a0 = 1, unsigned a1 = 1){
    if(n == 0)
        return a0;
    else if(n == 1)
        return a1;
    while(n-- > 1){
        unsigned int temp(a1);
        a1 += a0;
        a0 = temp;
    }
    return a1;
}


Then all you'd have to do is combine my example with Giblit's. Since you're dealing with pounds, you could easily edit my example to use floating point values if you need to.
Last edited on
Topic archived. No new replies allowed.