{"id":1232,"date":"2017-04-16T21:26:12","date_gmt":"2017-04-17T02:26:12","guid":{"rendered":"http:\/\/blog.kellie.wildroseandbriar.com\/?p=1232"},"modified":"2017-04-16T21:30:49","modified_gmt":"2017-04-17T02:30:49","slug":"fibonacci-fun-with-python","status":"publish","type":"post","link":"http:\/\/blog.kellie.wildroseandbriar.com\/?p=1232","title":{"rendered":"Fibonacci Fun with Python"},"content":{"rendered":"<p>I&#8217;ve been playing with numbers with Python. I wrote a function that returns the nth Fibonacci number. Fibonacci numbers are generated by taking zero and one as the first two numbers, then adding the previous two Fibonacci numbers together to yield the next one. The next Fibonacci number after 0 and 1 is also 1 since 0+1=1. The next one is 2. The next one is 3. The next one is 5. And so on. Strictly speaking, the first Fibonacci number is 1 since there is another trait that Fibonacci numbers exhibit and 0 can play no part in that. Given two successive Fibonacci numbers, the ratio of the greater to the lesser of the two will tend toward the golden mean or phi.<\/p>\n<p>The golden mean is approximately\u00a01.61803398875 although, like pi, it is an irrational quantity that has no exact expression. It can be written as the expression (1 + square_root(5))\/2. It is the square_root(5) term that makes the number irrational. But, if you will notice, 1\/1 is 1, 2\/1 is 2, 3\/2 is 1.5, 5\/3 is approximately 1.667 and as you continue to compute the ratio with larger and larger Fibonacci numbers, the result gets closer and closer to phi.<\/p>\n<p>Phi is an interesting quantity. It shows up in many places in nature. Some of the claims about it, for instance that it is evident in the logarithmic curve formed by a Nautilus shell, have been disproven by empirical study. Others, like the fact that the ratio of the length of a diagonal of a regular pentagon to the length of one of its sides is phi, are on sound footing. In any case, computing Fibonacci numbers and phi can be an interesting pastime.<\/p>\n<p>I found a simple function in Python that yields the nth Fibonacci number in relatively short order without having the unfortunate side effect of consuming the entire stack.<\/p>\n<style>.gist table { margin-bottom: 0; }<\/style>\n<div style=\"tab-size: 8\" id=\"gist46763690\" class=\"gist\">\n<div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n<div class=\"gist-data\">\n<div class=\"js-gist-file-update-container js-task-list-container\">\n<div id=\"file-gistfile1-txt\" class=\"file my-2\">\n<div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-text  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"gistfile1.txt content, created by javajini on 02:05AM on April 17, 2017.\"\n    ><\/p>\n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n<p>  <template class=\"js-file-alert-template\"><\/p>\n<div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n    <span><br \/>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br \/>\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a><br \/>\n    <\/span><\/p>\n<div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters<br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><\/template><br \/>\n<template class=\"js-line-alert-template\"><br \/>\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\"><br \/>\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n<\/span><\/template><\/p>\n<table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"gistfile1.txt\">\n<tr>\n<td id=\"file-gistfile1-txt-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n<td id=\"file-gistfile1-txt-LC1\" class=\"blob-code blob-code-inner js-file-line\">def fib(n):<\/td>\n<\/tr>\n<tr>\n<td id=\"file-gistfile1-txt-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n<td id=\"file-gistfile1-txt-LC2\" class=\"blob-code blob-code-inner js-file-line\">    a,b = 0, 1<\/td>\n<\/tr>\n<tr>\n<td id=\"file-gistfile1-txt-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n<td id=\"file-gistfile1-txt-LC3\" class=\"blob-code blob-code-inner js-file-line\">    for x in xrange(n):<\/td>\n<\/tr>\n<tr>\n<td id=\"file-gistfile1-txt-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n<td id=\"file-gistfile1-txt-LC4\" class=\"blob-code blob-code-inner js-file-line\">        a,b = b, a+b<\/td>\n<\/tr>\n<tr>\n<td id=\"file-gistfile1-txt-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n<td id=\"file-gistfile1-txt-LC5\" class=\"blob-code blob-code-inner js-file-line\">    return a<\/td>\n<\/tr>\n<\/table>\n<\/div><\/div>\n<\/p><\/div>\n<\/div><\/div>\n<div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/javajini\/6a3a3f89f0b3fe42da53740dbd40c272\/raw\/0a1dc7df3b959f85fd06ce3d10906c31221d10e7\/gistfile1.txt\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a><br \/>\n        <a href=\"https:\/\/gist.github.com\/javajini\/6a3a3f89f0b3fe42da53740dbd40c272#file-gistfile1-txt\" class=\"Link--inTextBlock\"><br \/>\n          gistfile1.txt<br \/>\n        <\/a><br \/>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n<\/p><\/div>\n<\/div>\n<p>This function takes an integer argument n. It initially sets a to 0 and b to 1. Python allows you to set multiple values with one assignment statement as shown in line 2 above. Then, for \u00a0a variable x assigned to every value between 0 and n-1, which is to say n times, you set a to b and b to (a+b). When you are done, you return a because it will be the nth Fibonacci number.<\/p>\n<p>I learned several things while writing this blog post. First, when you are computing Fibonacci numbers, the values get large very fast. For example, the 100th Fibonacci number is 21 digits long. The 1000th Fibonacci number is 209 digits long. Another thing I learned is that the square root symbol does not render as such in a standard web page. I am somewhat baffled by that as it renders fine in the WordPress editor. Perhaps I will discover a way around this.<\/p>\n<p>I also learned how to use GitHub to create a gist, that short block of code above. A gist allows you to display the program formatted as it would be in a code editor instead of formatted as prose as it would be if typed directly into the blog post.<\/p>\n<p>I learned other things tonight but I don&#8217;t intend to belabor this post with a lengthy explanation of everything I learned. Perhaps another night.<\/p>\n<hr \/>\n<p>Sweet dreams, don\u2019t forget to tell the ones you love that you love them, and most important of all, be kind.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing with numbers with Python. I wrote a function that returns the nth Fibonacci number. Fibonacci numbers are generated by taking zero and one as the first two numbers, then adding the previous two Fibonacci numbers together to yield the next one. The next Fibonacci number after 0 and 1 is also 1 &hellip; <a href=\"http:\/\/blog.kellie.wildroseandbriar.com\/?p=1232\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fibonacci Fun with Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[31],"tags":[251,252,250,244,245],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=\/wp\/v2\/posts\/1232"}],"collection":[{"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1232"}],"version-history":[{"count":4,"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=\/wp\/v2\/posts\/1232\/revisions"}],"predecessor-version":[{"id":1236,"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=\/wp\/v2\/posts\/1232\/revisions\/1236"}],"wp:attachment":[{"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1232"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.kellie.wildroseandbriar.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}