Created
June 1, 2011 02:31
-
-
Save SeanJA/1001696 to your computer and use it in GitHub Desktop.
Fibonacci Sequence iterator, because hey... why not?
This file contains hidden or 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 Fibonacci_Sequence implements Iterator { | |
| private $start; | |
| public function __construct($start=0){ | |
| $this->start = $start; | |
| } | |
| public function current() { | |
| return round( | |
| (pow(((1 + sqrt(5)) / 2), $this->key) - pow((-1 / (1 + sqrt(5)) / 2), $this->key)) / sqrt(5) | |
| ); | |
| } | |
| public function key() { | |
| return $this->key; | |
| } | |
| public function next() { | |
| $this->key++; | |
| } | |
| public function rewind() { | |
| $this->key = $this->start; | |
| } | |
| public function valid() { | |
| return true; | |
| } | |
| } | |
| $n = new Fibonacci_Sequence(10); | |
| $i = 0; | |
| foreach ($n as $value) { | |
| if ($i == 100) { | |
| break; | |
| } | |
| $i++; | |
| echo $value . PHP_EOL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment