読者です 読者をやめる 読者になる 読者になる

Engineer as a Lifestyle @tenkoma

tenkoma's testing blog

フィボナッチ数列

<?php

class TestFibonacci extends PHPUnit_Framework_TestCase
{
    function testFibonacci()
    {
        $cases = array(
            array(0, 0),
            array(1, 1),
            array(2, 1),
            array(3, 2),
            array(4, 3),
            array(5, 5),
            array(6, 8),
            array(7, 13),
            array(8, 21),
        );
        foreach($cases as $case) {
            $this->assertEquals($case[1], fib($case[0]));
        }
    }

}
function fib($int)
{
    if ($int == 0) return 0;
    if ($int == 1) return 1;
    return fib($int - 1) + fib($int - 2);
}

?>