{"id":583,"date":"2011-04-18T22:51:07","date_gmt":"2011-04-19T04:51:07","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=583"},"modified":"2018-07-01T23:14:14","modified_gmt":"2018-07-02T05:14:14","slug":"code-of-the-day-javascript-fibonacci-numbers","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/04\/code-of-the-day-javascript-fibonacci-numbers\/","title":{"rendered":"Code of the Day: Javascript Fibonacci Numbers the fast way"},"content":{"rendered":"<pre lang='javascript' line=\"1\">\r\n\/\/Programmer: Larry Battle\r\n\/\/Date: April 18, 2011\r\n\/\/Purpose: Get Fibonacci numbers in O(1) time.\r\nvar getFibonacciNum = function( n ){\r\n    var s5 = Math.sqrt(5);\r\n    var phi1 = (1+s5)\/2, phi2 = (1-s5)\/2;\r\n    return Math.round((Math.pow( phi1, n ) - Math.pow( phi2, n) )\/s5);\r\n};\r\n<\/pre>\n<p>Demo:<br \/>\n<iframe loading=\"lazy\" width=\"100%\" height=\"300\" src=\"\/\/jsfiddle.net\/jvVd4\/5\/embedded\/\" allowfullscreen=\"allowfullscreen\" allowpaymentrequest frameborder=\"0\"><\/iframe><\/p>\n<p>Output for 0 to 4<\/p>\n<pre>\r\nF(0) = 0\r\nF(1) = 1\r\nF(2) = 1\r\nF(3) = 2\r\nF(4) = 3\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/Programmer: Larry Battle \/\/Date: April 18, 2011 \/\/Purpose: Get Fibonacci numbers in O(1) time. var getFibonacciNum = function( n ){ var s5 = Math.sqrt(5); var phi1 = (1+s5)\/2, phi2 = (1-s5)\/2; return Math.round((Math.pow( phi1, n ) &#8211; Math.pow( phi2, n) )\/s5); }; Demo: Output for 0 to 4 F(0) = 0 F(1) = 1 F(2) &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/04\/code-of-the-day-javascript-fibonacci-numbers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript Fibonacci Numbers the fast way<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[164],"class_list":["post-583","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=583"}],"version-history":[{"count":7,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/583\/revisions"}],"predecessor-version":[{"id":1582,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/583\/revisions\/1582"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}