Iterated Fibonacci Sequence in PHP


The Iterator is an interface for external iterators or objects that can be iterated themselves internally. In PHP, it is defined as follows.

1
2
3
4
5
6
7
8
9
Iterator extends Traversable 
{
  /* Methods */
  abstract public mixed current ( void )
  abstract public scalar key ( void )
  abstract public void next ( void )
  abstract public void rewind ( void )
  abstract public boolean valid ( void )
}
Iterator extends Traversable 
{
  /* Methods */
  abstract public mixed current ( void )
  abstract public scalar key ( void )
  abstract public void next ( void )
  abstract public void rewind ( void )
  abstract public boolean valid ( void )
}

The following presents a simply clear example that computes the Fibonacci sequence using Iterator. The Fibonacci Number is very well known, probably due to its recursive definition. [read this]

By implementing the above six methods, the following class can be used as a iterator, e.g. in the foreach statement. The valid tells if the current position is OK, if not, the foreach loop will terminate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  class Fib implements Iterator 
  { 
    private $previous = 1; 
    private $current = 0; 
    private $key = 0; 
    
    public function __construct()
    {
        $this->rewind();
    }
       
    public function key() 
    { 
        return($this->key); 
    }
 
    public function current() 
    { 
        return($this->current); 
    } 
         
    public function next() 
    { 
        $n = $this->current; 
        $this->current += $this->previous; 
        $this->previous = $n; 
        $this->key++; 
    } 
    
    public function rewind() 
    { 
        $this->previous = 1; 
        $this->current = 0; 
        $this->key = 0; 
    } 
    
    public function valid() 
    { 
        return(true); 
    } 
  } 
 
  $seq = new Fib(); 
  $i = 0; 
  foreach ($seq as $f) 
  { 
    echo ($f); 
    if ($i++ === 10)
    {
      break;
    } 
  } 
  class Fib implements Iterator 
  { 
    private $previous = 1; 
    private $current = 0; 
    private $key = 0; 
    
    public function __construct()
    {
        $this->rewind();
    }
       
    public function key() 
    { 
        return($this->key); 
    }

    public function current() 
    { 
        return($this->current); 
    } 
         
    public function next() 
    { 
        $n = $this->current; 
        $this->current += $this->previous; 
        $this->previous = $n; 
        $this->key++; 
    } 
    
    public function rewind() 
    { 
        $this->previous = 1; 
        $this->current = 0; 
        $this->key = 0; 
    } 
    
    public function valid() 
    { 
        return(true); 
    } 
  } 

  $seq = new Fib(); 
  $i = 0; 
  foreach ($seq as $f) 
  { 
    echo ($f); 
    if ($i++ === 10)
    {
      break;
    } 
  } 

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
289 words
Last Post: Index Sorting in PHP
Next Post: Easy Timeit Function in Python

The Permanent URL is: Iterated Fibonacci Sequence in PHP (AMP Version)

Leave a Reply