{"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=\u201dbanner\u201d]\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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20python%20program%20to%20check%20if%20x%20is%20a%20perfect%20square%0Aimport%20math%0A%20%0A%23%20A%20utility%20function%20that%20returns%20true%20if%20x%20is%20perfect%20square%0Adef%20isPerfectSquare(x)%3A%0A%20%20%20%20s%20%3D%20int(math.sqrt(x))%0A%20%20%20%20return%20s*s%20%3D%3D%20x%0A%20%0A%23%20Returns%20true%20if%20n%20is%20a%20Fibinacci%20Number%2C%20else%20false%0Adef%20isFibonacci(n)%3A%0A%20%0A%20%20%20%20%23%20n%20is%20Fibinacci%20if%20one%20of%205*n*n%20%2B%204%20or%205*n*n%20-%204%20or%20both%0A%20%20%20%20%23%20is%20a%20perferct%20square%0A%20%20%20%20return%20isPerfectSquare(5*n*n%20%2B%204)%20or%20isPerfectSquare(5*n*n%20-%204)%0A%20%20%20%20%0A%23%20A%20utility%20function%20to%20test%20above%20functions%0Afor%20i%20in%20range(1%2C11)%3A%0A%20%20%20%20%20if%20(isFibonacci(i)%20%3D%3D%20True)%3A%0A%20%20%20%20%20%20%20%20%20print%20i%2C%22is%20a%20Fibonacci%20Number%22%0A%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20print%20i%2C%22is%20a%20not%20Fibonacci%20Number%20%22%0A\u201d message=\u201dPython Program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}