Created
January 27, 2011 20:50
-
-
Save davidjmemmett/799239 to your computer and use it in GitHub Desktop.
FibonnaciSequence class and PhiApproximator class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FibonnaciSequence implements Iterator { | |
private $index = 0; | |
protected $oldnum = 0; | |
private $currnum = 1; | |
private $nextnum = 1; | |
public function current() { | |
return $this->currnum; | |
} | |
public function next() { | |
$this->nextnum = $this->currnum + $this->oldnum; | |
$this->oldnum = $this->currnum; | |
$this->currnum = $this->nextnum; | |
$this->index++; | |
} | |
public function key() { | |
return $this->index; | |
} | |
public function valid() { | |
return true; | |
} | |
public function rewind() { | |
$this->index = 0; | |
$this->oldnum = 0; | |
$this->currnum = 1; | |
$this->nextnum = 1; | |
} | |
} | |
/* | |
$j = 0; | |
foreach (new FibonnaciSequence() as $idx => $i) { | |
printf("%u: %f, \n", $idx, $i); | |
if ($idx == 10) break; | |
} | |
*/ | |
class PhiApproximator extends FibonnaciSequence { | |
private $approximation = 0; | |
public function current() { | |
return $this->approximation; | |
} | |
public function next() { | |
parent::next(); | |
$this->approximation = bcdiv(sprintf("%f", parent::current()), sprintf("%f", ($this->oldnum == 0 ? 1 : $this->oldnum)), 10000); | |
} | |
public function rewind() { | |
parent::rewind(); | |
$this->approximation = 0; | |
} | |
} | |
$j = 0; | |
foreach (new PhiApproximator() as $idx => $i) { | |
printf("%u: %01.40f\n", $idx, $i); | |
if ($idx == 1475) break; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment