Created
February 22, 2011 09:51
-
-
Save wjzhangq/838433 to your computer and use it in GitHub Desktop.
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 Fib implements Iterator{ | |
var $a = 0; | |
var $b = 1; | |
var $i = 0; | |
var $max = 100; | |
public function __construct($n=100){ | |
$this->max = $n; | |
} | |
public function rewind(){ | |
$this->a = 0; | |
$this->b = 1; | |
$this->i = 0; | |
} | |
public function next(){ | |
$tmp = $this->a + $this->b; | |
$this->a = $this->b; | |
$this->b = $tmp; | |
$this->i = $this->i + 1; | |
} | |
public function current(){ | |
return $this->b; | |
} | |
public function key(){ | |
return $this->i; | |
} | |
public function valid(){ | |
return $this->b < $this->max; | |
} | |
} | |
$a = new Fib(100); | |
foreach($a as $k=>$v){ | |
echo sprintf("%d\t%d\n", $k, $v); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment