Home > php教程 > PHP源码 > PHP implements Fibonacci sequence

PHP implements Fibonacci sequence

WBOY
Release: 2016-08-04 08:53:34
Original
1656 people have browsed it
跳至 [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
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template