Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fibonacci's Rabbits Problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
isaiahk9




PostPosted: Tue Oct 14, 2008 6:58 am   Post subject: Fibonacci's Rabbits Problem

This was a problem several years ago in a programming competition. Since I've done all my work, my teacher put me to this :

----------------------------------------------------------------------------------------------------------------------------------------------------
The well known Fibonacci sequence 1,1,2,3,5,8,13,21,... is based on the proposition that, given that any pair of rabits will produce a new pair of rabits every month for ever, except in the first month after their birth. The sequence gives the number of pairs of rabits that there would be in any given month.

That is :
- starting with 1 newborn in month one
- which would be 1 pair of rabbits in month two
- which would be two pairs of rabits in month three (since this pair would produce a new pair in month three)
- which would be three pairs of rabits in month 4 (since the first pair would produce a new pair)
- which would be 5 pairs in month 5 (since the first two pairs would produce a new pair . . .
- etc.

But suppose that, instead of living forever, and reproducing each rabit could only live for 5 months, and therefore only produce 3 new pairs before they died. The sequence would then run like : 1,1,2,3,5,6,10,14...

And if each pair would only live for 3 months, the sequence would become : 1,1,2,1,2,1,2...

DATA.txt contains 5 lines of two positive integers, A and B. The integer A represents the number of months each rabit lives, 2 < A < 101. The integer B, 2 < B < 101, represents the number of months that have passed when the count of the number of pairs of rabits occurs. A and B will have values such that the number of pairs rabits will never exceed 999 999 999.

Output should be on a cleared screen or window, and will state the number of rabit pairs in each of the 5 cases. Output should appear as below.

SAMPLE INPUT SAMPLE OUTPUT
5 10 in problem #1 there are 30 rabit pair(s).
30 30 in problem #2 there are 832040 rabbit pair(s).
12 40 in problem #3 there are 95539192 rabbit pair(s).
4 40 in problem #4 there are 65657 rabbit pair(s).
6 49 in problem #5 there are 740444619 rabbit pair(s).

----------------------------------------------------------------------------------------------------------------------------------------------------

Here is the code I've got to make the fibonacci numbers :

----------------------------------------------------------------------------------------------------------------------------------------------------

% open and read file
var fileName : string := "DATA11.txt" % Name of file
var fileNo : int % Number of file
var input : string (100)
open : fileNo, fileName, get

% declare variables
var lifespan : int
var numMonths : int
var numRabbits1 : int
var numRabbits2 : int
var temp : int

% loop through five problems
for i : 1 .. 5

% get problem
get : fileNo, input : *

% get lifespan
lifespan := strint (input (1 .. index (input, " ") - 1))

% get numMonths
numMonths := strint (input (index (input, " ") + 1 .. *))

% Calculate numRabbit pairs
% loop for number of months
numRabbits1 := 1
numRabbits2 := 1

for j : 1 .. numMonths

% calculate number rabbits without deaths
temp := numRabbits2
numRabbits2 := numRabbits1 + numRabbits2
numRabbits1 := temp

% calculate number of deaths


end for

% output
put "in problem #", i, " there are ", numRabbits1, " pairs"


end for

close : fileNo

----------------------------------------------------------------------------------------------------------------------------------------------------

So, to find which rabbits to kill off, I've come up with this general idea :

When the fibbonnacci numbers are calculated, they are assigned a number for their generation (1 = generation1, 1 = generation2, 2 = generation3, 3 = generation4, 5 = generation5, 8 = generation6, etc.) Everytime the for loop goes through, there is another variable called currentgeneration which increases by one everytime. Then, if currentgeneration - generation > lifespan, the number of rabbits is minused one.

But how would I do that? I don't really know if an array would help, and string to variable conversion would work I think. . . So what do you think is the best way to go at this?
thanx
Sponsor
Sponsor
Sponsor
sponsor
S_Grimm




PostPosted: Tue Oct 14, 2008 7:30 am   Post subject: RE:Fibonacci\'s Rabbits Problem

a whole pile of "if else" statements. like if lifespan < age then ...
isaiahk9




PostPosted: Tue Oct 14, 2008 12:57 pm   Post subject: RE:Fibonacci\'s Rabbits Problem

yeah, but how do I tag the age number to the generation? that's the actual question.
Euphoracle




PostPosted: Tue Oct 14, 2008 3:30 pm   Post subject: RE:Fibonacci\'s Rabbits Problem

You could use a record:

Turing:
type Bunny :
record
    age, generation : int
end record

var q : Bunny
q.age := 35
q.generation := 2

put "Age:   ", q.age
put "Generation:    ", q.generation
isaiahk9




PostPosted: Tue Oct 14, 2008 4:32 pm   Post subject: RE:Fibonacci\'s Rabbits Problem

thanx Euphoracle - that looks like what I need, although I can't check it for a while
zylum




PostPosted: Wed Oct 15, 2008 3:40 am   Post subject: RE:Fibonacci\'s Rabbits Problem

You only need to keep track of how many rabbits of each age there are:

Turing:
const A := 6
const B := 49

var a : array 1 .. A + 1 of int
var n : int

for i : 2 .. A
    a (i) := 0
end for
a (1) := 2

for : 1 .. B
    n := 0
    for decreasing i : A .. 1
        n += a (i)
        a (i + 1) := a (i)
    end for

    %new rabbits = 2 * number of reproducing pairs
    %reproducing pairs = [total rabbits - newborn rabbits - dead rabbits] / 2
    a (1) := 2 * (n div 2 - a (1) div 2 - a (A + 1) div 2)
end for

put n div 2
isaiahk9




PostPosted: Wed Oct 15, 2008 4:37 am   Post subject: RE:Fibonacci\'s Rabbits Problem

thanx, zylum that looks perfect. Credit will go to you and Euphoracle for help.

. . . why did you respond to this at 4:40 am?
ecookman




PostPosted: Wed Oct 15, 2008 10:52 am   Post subject: RE:Fibonacci\'s Rabbits Problem

probally different timezone
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Wed Oct 15, 2008 12:08 pm   Post subject: Re: Fibonacci's Rabbits Problem

Some of us also have sleeping problems and caffeine addictions which is a good reason why we would reply at weird hours. Also some of us just like coding at weird times of the day. Remeber not everyone works 9 to 5 jobs Smile
isaiahk9




PostPosted: Wed Oct 15, 2008 3:57 pm   Post subject: RE:Fibonacci\'s Rabbits Problem

Yeah, I would be one of them who doesn't.
oh, well. thanx everyone for help with this problem, it is now officially solved (I took zylum's code, my code, and some new stuff and mashed it together to get the final product).
zylum




PostPosted: Thu Oct 16, 2008 3:18 am   Post subject: Re: RE:Fibonacci\'s Rabbits Problem

isaiahk9 @ Wed Oct 15, 2008 5:37 am wrote:
thanx, zylum that looks perfect. Credit will go to you and Euphoracle for help.

. . . why did you respond to this at 4:40 am?


Why did you respond at 5:37am? Laughing

To answer your question though, I am working on a contract for a client in the Netherlands thus their business hours are between 2am and 10am. I needed to have a chat with them and didn't feel like waking up Wink I usually have weird sleeping habits anyways..
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: