{"id":26021,"date":"2017-10-26T09:53:19","date_gmt":"2017-10-26T04:23:19","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26021"},"modified":"2017-10-26T09:53:19","modified_gmt":"2017-10-26T04:23:19","slug":"python-programming-check-given-number-fibonacci-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-check-given-number-fibonacci-number\/","title":{"rendered":"Python Programming -To check if a given number is Fibonacci number"},"content":{"rendered":"<p>Given a number \u2018n\u2019, how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ..<\/p>\n<p>Examples :<\/p>\n<p>Input : 8<br \/>\nOutput : Yes<\/p>\n<p>Input : 34<br \/>\nOutput : Yes<\/p>\n<p>Input : 41<br \/>\nOutput : No<\/p>\n<p>A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to \u2018n\u2019. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not.<\/p>\n[ad type=&#8221;banner&#8221;]\nA number is Fibonacci if and only if one or both of (5*n2 + 4) or (5*n2 \u2013 4) is a perfect square<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Program<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># python program to check if x is a perfect square<br\/>import math<br\/> <br\/># A utility function that returns true if x is perfect square<br\/>def isPerfectSquare(x):<br\/>    s = int(math.sqrt(x))<br\/>    return s*s == x<br\/> <br\/># Returns true if n is a Fibinacci Number, else false<br\/>def isFibonacci(n):<br\/> <br\/>    # n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both<br\/>    # is a perferct square<br\/>    return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)<br\/>    <br\/># A utility function to test above functions<br\/>for i in range(1,11):<br\/>     if (isFibonacci(i) == True):<br\/>         print i,&quot;is a Fibonacci Number&quot;<br\/>     else:<br\/>         print i,&quot;is a not Fibonacci Number &quot;<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>1 is a Fibonacci Number\r\n2 is a Fibonacci Number\r\n3 is a Fibonacci Number\r\n4 is a not Fibonacci Number\r\n5 is a Fibonacci Number\r\n6 is a not Fibonacci Number\r\n7 is a not Fibonacci Number\r\n8 is a Fibonacci Number\r\n9 is a not Fibonacci Number\r\n10 is a not Fibonacci Number<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Python Programming -To check if a given number is Fibonacci number &#8211; Mathematical Algorithms &#8211; A simple way is to generate Fibonacci numbers<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,74058,4148],"tags":[76665,3448,76646,76630,1742,76638,76636,76656,3437,76640,76632,76661,76653,76634,76654,76639,76658,76663,76657,76649,76659,76633,76667,76660,76668,76672,76651,76670,76664,76662,3449,76642,76631],"class_list":["post-26021","post","type-post","status-publish","format-standard","hentry","category-coding","category-mathematical-algorithms","category-python","tag-convert-string-to-datetime-python","tag-current-date-time","tag-date-python","tag-date-time","tag-datetime","tag-python-date-function","tag-python-date-to-timestamp","tag-python-datetime","tag-python-datetime-format","tag-python-datetime-now","tag-python-datetime-strptime-timezone","tag-python-datetime-to-string","tag-python-datetime-to-timestamp","tag-python-datetime-year","tag-python-format-example","tag-python-now","tag-python-strftime","tag-python-threading-example","tag-python-time","tag-python-time-module","tag-python-timedelta","tag-python-timer-example","tag-python-timestamp","tag-strftime-python","tag-strptime-python","tag-time-and-date-now","tag-time-and-date-today","tag-time-in-python","tag-time-python","tag-timedelta","tag-timedelta-python","tag-today-date-time","tag-today-time-and-date"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26021"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26021\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}