{"id":183149,"date":"2026-02-18T11:17:53","date_gmt":"2026-02-18T10:17:53","guid":{"rendered":"https:\/\/liora.io\/en\/?p=183149"},"modified":"2026-02-18T11:17:53","modified_gmt":"2026-02-18T10:17:53","slug":"fibonacci-sequence-recursion-cryptography-and-the-golden-ratio","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/fibonacci-sequence-recursion-cryptography-and-the-golden-ratio","title":{"rendered":"Fibonacci sequence: Recursion, cryptography and the golden ratio"},"content":{"rendered":"<p><strong>In the world of mathematics, the importance of sequences and series in analysis is well established. Sometimes, it&#8217;s hard to find a concrete application for concepts invented (or discovered?) in the field. The Fibonacci sequence, on the other hand, can be found in many areas of nature, and continues to fascinate researchers thanks to its properties closely linked to the golden ratio.<\/strong><\/p>\n<!-- \/wp:post-content -->\n\n<!-- wp:heading -->\n<h2 id=\"h-history-and-background\" class=\"wp-block-heading\">History and background<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>The<strong> Fibonacci sequence<\/strong> takes its name from the 13th-century Italian mathematician Leonardo of Pisa, also known as Fibonacci. Although he didn&#8217;t invent the sequence, he introduced it to Europe with his book &#8220;Liber Abaci&#8221; (Book of the Abacus or Book of Calculation) in 1202.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>In this book, <strong>Fibonacci<\/strong> posed and solved a problem involving the growth of an idealized rabbit population, which leads to this sequence of numbers. It&#8217;s worth noting that the Fibonacci sequence was known long before this date in India, <strong>where the relationships between the numbers in the sequence were already being studied.<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Fibonacci numbers have many applications in nature and art. They can be observed on a variety of scales, from flower petals to galaxy spirals. Flower petals often have <strong>Fibonacci<\/strong> numbers &#8211; daisies can have 34, 55 or even 89 petals. And sunflower seeds or pine cones are often arranged in spirals following Fibonacci numbers.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>In architecture, the golden ratio, directly linked to the <strong>Fibonacci sequence<\/strong>, has been used to create works that are pleasing to the eye thanks to their &#8220;perfect&#8221; proportions. For example, the Parthenon in Greece and the Taj Mahal in India both seem to use the golden ratio in their construction.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>It&#8217;s this universality that makes the Fibonacci sequence so fascinatin<\/strong>g &#8211; a simple sequence of numbers that finds its way through mathematics, nature and art, uniting fields that might seem remote at first glance. In the next section, we&#8217;ll take a closer look at what these numbers are and how we can generate them ourselves using the <a href=\"https:\/\/liora.io\/en\/python-the-most-popular-language\">Python programming language.<\/a><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"width\":\"1000px\",\"height\":\"auto\",\"style\":{\"spacing\":{\"margin\":{\"top\":\"var:preset|spacing|columns\",\"bottom\":\"var:preset|spacing|columns\"}}}} -->\n\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Romanesco cabbage hides a secret &#8211; count the number of spirals, if you can!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:buttons {\"className\":\"is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"} -->\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><!-- wp:button -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-scientist\">A better understanding of the Fibonacci sequence<\/a><\/div>\n<!-- \/wp:button --><\/div>\n<!-- \/wp:buttons -->\n\n<!-- wp:heading -->\n<h2 class=\"wp-block-heading\">Definition and implementation<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Formally, the <strong>Fibonacci sequence<\/strong> is a sequence in which each term is equal to the sum of the two preceding elements. Just knowing the first two terms and the recurrence formula is enough to construct the sequence in its entirety.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>$F_0 = 0$, $F_1 = 1$ and $F_n = F_{n-1}+ F_{n-2}$ for $n geq 2$.<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Building a program to provide the nth <strong>Fibonacci number<\/strong> is a very good elementary exercise. Before reading on, you can try coding the function yourself!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Here&#8217;s a Python function to solve the problem:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:code {\"style\":{\"spacing\":{\"margin\":{\"top\":\"var:preset|spacing|columns\",\"bottom\":\"var:preset|spacing|columns\"}}},\"fontSize\":\"xsmall\"} -->\n<pre class=\"wp-block-code has-xsmall-font-size\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\"><code>\t\t\t\t\tdef fibonacci(n):\n    if n in {0, 1}:\n        return n\n    return fibonacci(n-1) + fibonacci(n-2)\t\t\t\t<\/code><\/pre>\n<!-- \/wp:code -->\n\n<!-- wp:paragraph -->\n<p>This<strong> initial recursive implementation may be satisfactory<\/strong>, but there is scope for optimizing this code by opting for an iterative approach. Recursion is very restrictive in terms of computation time; in fact, the time complexity of this function is O(n\u00b2). In practice, this means that the function struggles to deliver a result within a satisfactory timeframe when n is large (for example, n = 40).<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Here&#8217;s a new approach, this time iterative, which produces the same result using a for loop:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:code {\"style\":{\"spacing\":{\"margin\":{\"top\":\"var:preset|spacing|columns\",\"bottom\":\"var:preset|spacing|columns\"}}},\"fontSize\":\"xsmall\"} -->\n<pre class=\"wp-block-code has-xsmall-font-size\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\"><code>\n\t\t\t\t\tdef fibonacci(n):\n    a, b, c= 0, 1, 0\n    if n == 0:\n        return 0\n    for i in range(2, n+1):\n        c = a + b\n        a = b\n        b = c\n    return b<\/code><\/pre>\n<!-- \/wp:code -->\n\n<!-- wp:paragraph -->\n<p>One way of optimizing this code would be to cache the elements of the sequence as they are introduced, so as to avoid redundant calculations each time the function is called.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>In addition, there&#8217;s a mathematical formula for calculating the exact term of the sequence without recursion. Called Binet&#8217;s formula, it uses the golden ratio :<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:code {\"fontSize\":\"xsmall\"} -->\n<pre class=\"wp-block-code has-xsmall-font-size\"><code>\t\t\t\t\tdef fibonacci(n):\n    sqrt5 = math.sqrt(5)\n    F_n = int((( (1 + sqrt5) ** n - (1 - sqrt5) ** n ) \/ ( 2 ** n * sqrt5 )))\n    return F_n\t\t<\/code><\/pre>\n<!-- \/wp:code -->\n\n<!-- wp:heading -->\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>The<strong> Fibonacci sequence<\/strong> is used in a wide variety of fields, such as cryptography and trading. Among other things, it can be used to generate a list of pseudo-random numbers or to create an elementary encryption system.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>In finance, a tool called the Fibonacci retracement level is used to estimate how far an asset could fall before resuming its trend movement. In time series analysis, <strong>Fibonacci<\/strong> numbers are used to determine the optimum number of time periods to use in calculating moving averages.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:buttons {\"className\":\"is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"} -->\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><!-- wp:button -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-scientist\">Mastering the Fibonacci sequence in Data Science<\/a><\/div>\n<!-- \/wp:button --><\/div>\n<!-- \/wp:buttons -->\n\n<!-- wp:html -->\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What is the Fibonacci sequence?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"The Fibonacci sequence is a numerical series in which each number is the sum of the two preceding ones, typically starting with 0 and 1.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How is recursion used to calculate the Fibonacci sequence?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Recursion calculates the Fibonacci sequence by defining each term as the sum of the two previous terms, calling the same function repeatedly until reaching the base cases.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What is the link between the Fibonacci sequence and the golden ratio?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"The ratio between consecutive Fibonacci numbers converges toward the golden ratio, approximately equal to 1.618, as the sequence progresses.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How is the Fibonacci sequence used in cryptography?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"The Fibonacci sequence can be used in cryptography through mathematical properties and number patterns that support encryption algorithms and secure key generation.\"\n      }\n    }\n  ]\n}\n<\/script>\n<!-- \/wp:html -->","protected":false},"excerpt":{"rendered":"<p>In the world of mathematics, the importance of sequences and series in analysis is well established. Sometimes, it&#8217;s hard to find a concrete application for concepts invented (or discovered?) in the field. The Fibonacci sequence, on the other hand, can be found in many areas of nature, and continues to fascinate researchers thanks to its [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":207171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2426],"class_list":["post-183149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cybersecurity"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=183149"}],"version-history":[{"count":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183149\/revisions"}],"predecessor-version":[{"id":207172,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183149\/revisions\/207172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/207171"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=183149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=183149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}