aboutsummaryrefslogtreecommitdiff
path: root/challenge-150/mohammad-anwar/perl/ch-1.pl
blob: dfdbced2ad5e4e934aa308431018e48c6495ce0c (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
36
37
#!/usr/bin/perl

=head1

Week 150:

    https://theweeklychallenge.org/blog/perl-weekly-challenge-150

Task #1: Fibonacci Words

    You are given two strings having same number of digits, $a and $b.

    Write a script to generate Fibonacci Words by concatenation of the previous two strings. Finally print 51st digit of the first term having at least 51 digits.

=cut

use strict;
use warnings;
use Test::More;

is(fibonacci_words('1234', '5678', 51), 7, 'Example');

done_testing;

#
#
# METHOD

sub fibonacci_words {
    my ($term_1, $term_2, $index) = @_;

    while (length($term_1 . $term_2) <= $index) {
        ($term_1, $term_2) = ($term_2, $term_1 . $term_2);
    }

    return substr($term_1 . $term_2, --$index, 1);
}