fibonacci series


See On Github

Data

Contributor

Generic placeholder thumbnail

by Sweet-kid

in perl

Tags

recursion

Source Code

package Fibonacci;

use strict;
use warnings;

sub fibonacci {
    my $number = shift;

    if( $number < 2 ) {
        return 1;
    } else {
        fibonacci($number - 1) + fibonacci( $number - 2);
    }
}

1;
use Test::More;

use Fibonacci;

is( Fibonacci::fibonacci(0),
    1,
    "fibonacci(0) = 1");

is( Fibonacci::fibonacci(1),
    1,
    "fibonacci(1) = 1");

is( Fibonacci::fibonacci(2),
    2,
    "fibonacci(2) = 2" );

is( Fibonacci::fibonacci(5),
    8,
    "fibonacci(5) = 8" );

is( Fibonacci::fibonacci(10),
    89,
    "fibonacci(10) = 89" );

done_testing;