Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Created June 1, 2011 02:31
Show Gist options
  • Select an option

  • Save SeanJA/1001696 to your computer and use it in GitHub Desktop.

Select an option

Save SeanJA/1001696 to your computer and use it in GitHub Desktop.
Fibonacci Sequence iterator, because hey... why not?
<?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