コード例 #1
0
    }
    public function key()
    {
        return $this->key;
    }
    public function next()
    {
        if ($this->value === 0) {
            $this->value = 1;
        } else {
            $old = $this->value;
            $this->value += $this->sum;
            $this->sum = $old;
        }
        $this->key++;
    }
    public function valid()
    {
        return $this->value < PHP_INT_MAX;
    }
}
// print the Fibonacci numbers until PHP_INT_MAX
foreach ($test = new Fibonacci() as $key => $value) {
    printf("%d) %d\n", $key, $value);
}
// print the first 10 Fibonacci's numbers
$num = new Fibonacci();
for ($i = 0; $i < 10; $i++) {
    printf("%d) %d\n", $i, $num->current());
    $num->next();
}
コード例 #2
0
    public function current()
    {
        return $this->value;
    }
    public function key()
    {
        return $this->key;
    }
    public function next()
    {
        if ($this->value === 0) {
            $this->value = 1;
        } else {
            $old = $this->value;
            $this->value += $this->sum;
            $this->sum = $old;
        }
        $this->key++;
    }
    public function valid()
    {
        return true;
    }
}
// check the first 10 Fibonacci's numbers
$num = new Fibonacci();
$correct = array(0, 1, 1, 2, 3, 5, 8, 13, 21, 34);
for ($i = 0; $i < 10; $i++) {
    printf("%s<br>", $num->current() === $correct[$i] ? 'OK ' : 'ERROR');
    $num->next();
}