.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/combinatorics/sequences/plot_01_fibonacci_rabbits.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_combinatorics_sequences_plot_01_fibonacci_rabbits.py: Fibonacci's rabbits and domino tilings ============================================ Reproduces the rabbit problem from Fibonacci's 1202 *Liber Abaci*, checks that the same numbers count domino tilings of a 2-by-n strip, and shows the ratio of successive terms approaching the golden ratio. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.combinatorics import domino_tilings, fibonacci .. GENERATED FROM PYTHON SOURCE LINES 17-19 Rabbit pairs month by month ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python # Liber Abaci starts from one pair and counts the pairs after each month. print("month: " + " ".join(f"{m:4d}" for m in range(0, 13))) print("rabbit pairs:" + " ".join(f"{fibonacci(m + 2):4d}" for m in range(0, 13))) print(f"after a year: {fibonacci(14)} pairs, Fibonacci's own answer") .. rst-class:: sphx-glr-script-out .. code-block:: none month: 0 1 2 3 4 5 6 7 8 9 10 11 12 rabbit pairs: 1 2 3 5 8 13 21 34 55 89 144 233 377 after a year: 377 pairs, Fibonacci's own answer .. GENERATED FROM PYTHON SOURCE LINES 26-28 Tilings of a 2-by-n strip ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python for n in range(1, 8): print(f"2 x {n} strip: {domino_tilings(n)} tilings") .. rst-class:: sphx-glr-script-out .. code-block:: none 2 x 1 strip: 1 tilings 2 x 2 strip: 2 tilings 2 x 3 strip: 3 tilings 2 x 4 strip: 5 tilings 2 x 5 strip: 8 tilings 2 x 6 strip: 13 tilings 2 x 7 strip: 21 tilings .. GENERATED FROM PYTHON SOURCE LINES 33-35 Ratios converge to the golden ratio ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 35-44 .. code-block:: Python n = np.arange(2, 30) ratios = [fibonacci(k + 1) / fibonacci(k) for k in n] phi = (1 + np.sqrt(5)) / 2 fig, ax = plt.subplots() ax.semilogy(n, [abs(r - phi) for r in ratios], "o-") ax.set_xlabel("n") ax.set_ylabel(r"$|F_{n+1}/F_n - \varphi|$") ax.set_title("Successive ratios approach the golden ratio") .. image-sg:: /api/gallery/combinatorics/sequences/images/sphx_glr_plot_01_fibonacci_rabbits_001.png :alt: Successive ratios approach the golden ratio :srcset: /api/gallery/combinatorics/sequences/images/sphx_glr_plot_01_fibonacci_rabbits_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Successive ratios approach the golden ratio') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.031 seconds) .. _sphx_glr_download_api_gallery_combinatorics_sequences_plot_01_fibonacci_rabbits.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_fibonacci_rabbits.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_fibonacci_rabbits.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_fibonacci_rabbits.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_