{"id":40,"date":"2008-07-11T18:19:46","date_gmt":"2008-07-11T16:19:46","guid":{"rendered":"http:\/\/www.juust.org\/?page_id=40"},"modified":"2021-11-09T07:58:12","modified_gmt":"2021-11-09T05:58:12","slug":"php-fibonacci-class","status":"publish","type":"page","link":"https:\/\/www.juust.org\/index.php\/php-classes\/php-fibonacci-class\/","title":{"rendered":"fibonacci class"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignleft\" style=\"float: left; margin-left: 10px; margin-right: 10px;\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/4\/44\/Helianthus_whorl.jpg\" alt=\"wikipedia fibonacci sunflower\" width=\"160\" height=\"120\" \/><\/p>\n<p>A fibonacci tree, done with a php class. I first checked the sites on fibonacci routines but could not find a php version I could comprehend so I made one with some simple classes :<\/p>\n<pre lang=\"php\">Class Fibonacci {\n\n  var $Fibs = array();\n\n  public function __construct($code) {\n               $this-&gt;recurse($code);\n  }\n\n  public function recurse($n) {\n\t if($n==1 || $n==2) {\n\t    $this-&gt;Fibs[$n]-&gt;myValue = 1;\n\t  \treturn 1;\n\t\t} else {\n\t\t  $this-&gt;recurse($n-1);\n\t\t  $this-&gt;recurse($n-2);\n\t\t  $this-&gt;Fibs[$n]-&gt;myValue = $this-&gt;Fibs[$n-1]-&gt;myValue + $this-&gt;Fibs[$n-2]-&gt;myValue;\n\n\t\treturn $this-&gt;Fibs[$n]-&gt;myValue;\n\t}\n}\n\n \tpublic function Fibs($code) {\n\t        if (!$this-&gt;Fibs[$code])\n\t       {\n\t           $this-&gt;Fibs[$code] = new Fib($code);\n \t       }\n\t       return $this-&gt;Fibs[$code];\n\t}\n\n}\n\nClass Fib {\n    var $myFib;\n    var $myValue;\n\n  public function __construct($code) {\n               $this-&gt;myFib = $code;\n  }\n\n}<\/pre>\n<p>I was rather surprised when it actually worked.<\/p>\n<p>It iterates once and digs down to nodes(0, 1), returns the values of the two preceding nodes as any node&#8217;s value and sums back up to the node I enter. This way it calculates the tree once, then I retrieve values by referencing a node.<\/p>\n<pre lang=\"php\">$F = new Fibonacci(7);\necho $F-&gt;Fibs(7)-&gt;myValue;\necho $F-&gt;Fibs(6)-&gt;myValue;<\/pre>\n<p>here is a normal sample from the <a href=\"http:\/\/www.ibm.com\/developerworks\/opensource\/library\/os-php-fastapps2\/\" rel=\"nofollow noopener\" target=\"_blank\">IBM site <\/a><\/p>\n<pre lang=\"php\">function fib($nth = 1) {\n  static $fibs = array();\n\n  if ( ! empty ($fibs[$nth] ) ) {\n    return( $fibs[$nth] );\n  }\n\n  if ( $nth &lt; 2 ) {\n    $fibs[$nth] = $nth;\n  }\n  else {\n    $fibs[$nth - 1] = fib( $nth - 1 );\n    $fibs[$nth - 2] = fib( $nth - 2 );\n    $fibs[$nth] = $fibs[$nth - 1] + $fibs[$nth -2];\n  }\n\n  return( $fibs[$nth] );\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A fibonacci tree, done with a php class. I first checked the sites on fibonacci routines but could not find a php version I could comprehend so I made one with some simple classes : Class Fibonacci { var $Fibs = array(); public function __construct($code) { $this-&gt;recurse($code); } public function recurse($n) { if($n==1 || $n==2) [&hellip;]<\/p>\n","protected":false},"author":5796,"featured_media":0,"parent":83,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-40","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/pages\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/users\/5796"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":1,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/pages\/40\/revisions"}],"predecessor-version":[{"id":6594,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/pages\/40\/revisions\/6594"}],"up":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/pages\/83"}],"wp:attachment":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}