Fibonacci 数列

来源:互联网 发布:showgirl是什么软件 编辑:程序博客网 时间:2024/06/08 17:51
<?php/** * Fibonacci 数列 */class Fibonacci implements Iterator{    private $_previous = 0;    private $_current = 1;    private $_position = 0;        public function current()    {        return $this->_current;    }        public function key()    {        return $this->_position;    }        public function next()    {        $current = $this->_current;        $this->_current+= $this->_previous;        $this->_previous = $current;        $this->_position++;    }        public function rewind()    {        $this->_previous = 0;        $this->_current = 1;        $this->_position = 0;    }        public function valid()    {        return true;    }}$seq = new Fibonacci;$i = 0;foreach ($seq as $f) {    echo "$f \n";    if ($i++ === 19) break;}

0 0