Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Rabbits

by CloneArmyCommander (Friar)
on Nov 19, 2004 at 17:34 UTC ( [id://409072]=obfuscated: print w/replies, xml ) Need Help??

This is my first time posting here. I don't know how much it could be considered obfuscation, but this is my first attempt. It's simple. It's the answer to the problem that is on everyone's mind. If we start with two immortal rabbits, and they have two children every month year, how many rabbits will we have after nth month. The answer, of coarse, is the Fibonacci sequence :). This script takes one command line argument. Plug in a number for the nth month and it should print the sequence up to that month :). Sorry for the simplicity, and the bad ASCII art (I had to write it out without help from an ASCII art generator).
#!/usr/bin/perl -w ;;;;; ;;;;; ;;;;;;;;; ;;;;;;;;;;;; use strict; fib($ARGV[0]||42); sub fib { my @fibs=(1,1,0);; for(my $n=2;$n <$_[0];$n++) { $fibs[$n]=$fibs [$n-1]+$fibs [$n-2]; }my $sequence =@fibs; print "{"; for(my $x=0; $x<= $sequence; $x++) { if($x ==$sequence) { print ". . .}"; }else { print $fibs[$x]; print ",";} }print "\n";} ; ; ; ; ; ; ;;;;;;;;;;;

Replies are listed 'Best First'.
Re: Rabbits
by Limbic~Region (Chancellor) on Nov 19, 2004 at 19:03 UTC
    CloneArmyCommander,
    It doesn't work for me.
    $ perl rabbit.pl Use of uninitialized value in numeric lt (<) at ./rabbit.pl line 7. {1,1,0,. . .} $ perl -v This is perl, v5.8.5 built for cygwin-thread-multi-64int

    Cheers - L~R

    Update: I guess it helps if you supply it a command line argument - Sorry. To avoid this I would adjust the ASCII with the following change:
    fib($ARGV[0]) to fib($ARGV[0]||42)
      Sounds like a nice even number :), I usually tested it with 10, but I was not sure how much people wanted to see of it.
      Sorry for that, I probably should have made that clear :). I was in a hurry when I posted it, so I am still in the process of cleaning up grammar and spelling (the down side to Perl value of being impatient ;). I hope my code has some sort of value :).
Re: Rabbits
by Jasper (Chaplain) on Nov 24, 2004 at 17:22 UTC
    Umm, nice ASCII art, and all, but the whole rabbits thing isn't the fibonacci sequence, is it?

    If I start with two rabbits, and each pair produces two more each month, then I'll end up with 2, 4, 8, 16, 32, 64... rabbits after each month.

    Won't I?
      That's the story behind that Fibonacci sequence. I forget the exact details, but many years ago Fibonacci was given this problem, "If I have two rabbits. . . " and the solution he found was, 1,1,2,3,5,8,.... It also gave me an image to use :). Another interesting thing is that things in nature, such as flowers, seem to show numbers relating to the sequence. Almost all flowers have a number of petals that is a number in the Fibonacci sequence :) (normally, pluck a few out, and I might be wrong ;).

        Let's assume that each couple of rabbits give birth to two baby rabbits.

        Let's count the number of couple of rabbits. At the beginning, we've 1 couple of young rabbit

        F_1 = 1

        Next year, they'll be adult rabbits, but won't have children yet :

        F_2 = 1

        On the third year, they'll a two baby rabbits, that is one couple

        F_3 = 2

        On the forth year, the first parent will have two baby rabbits again, but young rabbits won't be old enough to do so

        F_3 = 2 + 1 = 3

        On the fifth year, we've got two couples that can have little rabbits and one that cannot :

        F_3 = 3 + 2 = 5

        and so on... Each year, the number of couple is :

        • the number of rabbits that lived the year before, that is F_{n-1}
        • plus the number of rabbits that were born that year, that is the number of couple old enough to proceate, that is F_{n-2}

        To conclude :

        F_n = F_{n-1} - F{n-2}

        Funny, isn't it ? Ok, that is not very realistic and very accurante, but who cares ? ;)


        --
        zejames

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://409072]
Approved by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2025-11-13 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.