Guest User

Untitled

a guest
Jul 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. <?php
  2. class Fib implements Iterator{
  3. var $a = 0;
  4. var $b = 1;
  5. var $i = 0;
  6. var $max = 100;
  7.  
  8. public function __construct($n=100){
  9. $this->max = $n;
  10. }
  11.  
  12. public function rewind(){
  13. $this->a = 0;
  14. $this->b = 1;
  15. $this->i = 0;
  16. }
  17.  
  18. public function next(){
  19. $tmp = $this->a + $this->b;
  20. $this->a = $this->b;
  21. $this->b = $tmp;
  22. $this->i = $this->i + 1;
  23. }
  24.  
  25. public function current(){
  26. return $this->b;
  27. }
  28.  
  29. public function key(){
  30. return $this->i;
  31. }
  32.  
  33. public function valid(){
  34. return $this->b < $this->max;
  35. }
  36. }
  37.  
  38. $a = new Fib(100);
  39. foreach($a as $k=>$v){
  40. echo sprintf("%d\t%d\n", $k, $v);
  41. }
  42.  
  43. ?>
Add Comment
Please, Sign In to add comment