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

Fall Hiking - October 2025 - Utah

 

New 3D printing model! Circle Mosaic Rainbow Puzzle.

Available on MakerWorld: https://makerworld.com/en/models/1960502-circle-mosaic-rainbow-puzzle Public domain for your use. If you are selling online, I would appreciate if you mention that you got the model from me, Amanda Roos. Enjoy! Fits snuggly in 3x3 inch bags!

Halloween Minis!

 Thanks to everyone for boosting my mini Halloween 3D print models on MakerWorld! They are my most popular models yet! Link to pumpkins: https://makerworld.com/models/1746189 Link to ghosts: https://makerworld.com/models/1743420 I also got a five star review of my rainbow mini ghost packs on my Etsy shop! https://amandaroosart.etsy.com