aboutsummaryrefslogtreecommitdiff
path: root/challenge-155/e-choroba/perl/ch-2.pl
blob: b6b6f9bcc1d433d1da0f048bde72d3e828405bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#! /usr/bin/perl
use warnings;
use strict;

{   package Pisano;
    sub new  {
        my ($class, $n) = @_;
        bless [$n, [1, 1]], $class
    }

    sub Next {
        my ($self) = @_;
        push @{ $self->[1] },
            ((my $f = shift @{ $self->[1] }) + $self->[1][0]) % $self->[0];
        return $f
    }
}

sub pisano_period {
    my ($n) = @_;
    my $f = 'Pisano'->new($n);
    my $period = "0";
    until ($period =~ /^(.+) \1/) {
        $period .= ' ' . ($f->Next);
    }
    my $length = split / /, $period;
    return $length / 2
}

use Test::More tests => 4;

is pisano_period(3), 8, 'pi(3)';
is pisano_period(2), 3, 'pi(2)';
is pisano_period(10), 60, 'pi(10)';
is pisano_period(50), 300, 'pi(50)';