juust ~ php oddities

Unordered list of one element
  • rss
  • begin
  • about
    • vcard
    • WTF is BroJesus
  • php scripts
    • flickr wp widget
    • google multi key serp tool, php script
    • gwt plugin
  • php classes
    • php pagerank class
    • fibonacci class
    • robots.txt parser php class
  • serp
    • serp dashboard wordpress plugin
  • services

fibonacci class

wikipedia fibonacci sunflower

Of my other site : one of them brainbreakers, 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 {
  1.  
  2.   var $Fibs = array();
  3.  
  4.   public function __construct($code) {
  5.                $this->recurse($code);
  6.   }
  7.  
  8.   public function recurse($n) {
  9.   if($n==1 || $n==2) {
  10.      $this->Fibs[$n]->myValue = 1;
  11.     return 1;
  12.   } else {
  13.     $this->recurse($n-1);
  14.     $this->recurse($n-2);
  15.     $this->Fibs[$n]->myValue = $this->Fibs[$n-1]->myValue + $this->Fibs[$n-2]->myValue;
  16.  
  17.   return $this->Fibs[$n]->myValue;
  18.  }
  19. }
  20.  
  21.   public function Fibs($code) {
  22.          if (!$this->Fibs[$code])
  23.         {
  24.             $this->Fibs[$code] = new Fib($code);
  25.          }
  26.         return $this->Fibs[$code];
  27.  }
  28.  
  29. }
  30.  
  31. Class Fib {
  32.     var $myFib;
  33.     var $myValue;
  34.  
  35.   public function __construct($code) {
  36.                $this->myFib = $code;
  37.   }
  38.  
  39. }

I was rather surprised when it actually worked.

It iterates once and digs down to nodes(0, 1), returns the values of the two preceding nodes as any node’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.

$F = new Fibonacci(7);
  1. echo $F->Fibs(7)->myValue;
  2. echo $F->Fibs(6)->myValue;

here is a normal sample from the IBM site

function fib($nth = 1) {
  1.   static $fibs = array();
  2.  
  3.   if ( ! empty ($fibs[$nth] ) ) {
  4.     return( $fibs[$nth] );
  5.   }
  6.  
  7.   if ( $nth < 2 ) {
  8.     $fibs[$nth] = $nth;
  9.   }
  10.   else {
  11.     $fibs[$nth - 1] = fib( $nth - 1 );
  12.     $fibs[$nth - 2] = fib( $nth - 2 );
  13.     $fibs[$nth] = $fibs[$nth - 1] + $fibs[$nth -2];
  14.   }
  15.  
  16.   return( $fibs[$nth] );
  17. }
         
Tags
class, php
Comments rss
Comments rss
Trackback
Trackback

Leave a Reply

Click here to cancel reply.

Recent Posts

  • p2p with wordpress xml-rpc
  • Tweets on Google’s frontpage
  • happy new year
  • metaWeblog.newPost posting to Wordpress from Word
  • IE is retarded

click me!
rss
Comments rss
Blog Directory
Web Developement Blogs - BlogCatalog Blog Directory
Listed in LS Blogs the Blog Directory and Blog Search Engine
Blog Flux Directory
joopita.com free web directory and search engine
design by jide
sitemap
8409 confirmed spam kills