Skip to main content

Recursive Fibonacci Rabbits with Python

Let's say we have some magical rabbits. These rabbits never die. A pair of rabbits has 3 more pairs of rabbits in every litter, and they start producing litters after one generation. If you start with one pair of rabbits, how many rabbits will you have in 5 generations?

This was the premise of a problem I was working on this week in Python (check out the full problem here: http://rosalind.info/problems/fib/). I had to draw it out on my white board to wrap my head around it:



The idea behind this problem is that finding the number of rabbits in a given generation is similar to calculating a given number in the Fibonacci sequence and that it can be solved recursively, so that's how I did it.

Recursive rabbit solution:


It doesn't really have to be done recursively, though. I was talking through it with Steven, and we realized that the manual approach I had used on the whiteboard was also fairly efficient. Turns out it can be implemented in fewer lines of code as well.

Non-recursive rabbit solution:

Comments

Popular posts from this blog

Build the life you want to live. The simple things matter.

"Build the life you want to live" is a phrase that changed my life.  I learned it while stuck in a soul-sucking accounting job. I was feeling disillusioned with my new career. I wanted to help people directly, not make rich corporations even richer.  I made a pivot. I learned to code Android apps and uploaded a handful of them to the Google Play Store.  Coding didn't stick as a new career path for me, but of all the things I've made in the past decade, those little apps still make me so proud and happy.  I finally felt like I was helping people. I received a lot of nice feedback from people all over the world. One person even told me they used my little Change Calculator to help their kid get a job. I made that app 8 years ago, and I love to see people are still using it. I'm learning that "building the life I want to live"  doesn't mean I have to wait for some grand moment when everything finally comes together. Even the small stuff I'm building...

New Etsy item! Cute Panda Earrings by Amanda Roos

  Check them out in my Etsy shop: https://amandaroosart.etsy.com

Impeach and remove Trump. No more endless war.

 The Trump administration attacked Iran today. This is not ok. Protest tomorrow, Sunday, June 22, 2025. Anywhere. Noon. Make a sign. Impeach and remove Trump now.  Call your senators and representatives. 5calls.org makes it easy. Send physical mail. Don't let up.  War is expensive.  Not just money.  HUMAN LIVES. The sign I painted: