Home  >  Article  >  php教程  >  PHP implements Fibonacci sequence

PHP implements Fibonacci sequence

WBOY
WBOYOriginal
2016-08-04 08:53:341624browse
跳至 [1] [全屏预览]
<?php
// fib.php  
function fib($n)
{
    $cur = 1;
    $prev = 0;
    for ($i = 0; $i < $n; $i++) 
    {
        yield $cur;

        $temp = $cur;
        $cur = $prev + $cur;
        $prev = $temp;
    }
}

$fibs = fib(19);
foreach ($fibs as $fib) {
    echo " " . $fib;
}

# php fib.php
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn