diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-01-30 02:19:23 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-01-30 02:19:23 +0000 |
| commit | 2232cab693b42b73fbf94508a6ec0da9c0fea906 (patch) | |
| tree | 5b3aad590f80161dd00a9533ae8db77a6cf89295 /challenge-149/mohammad-anwar/perl | |
| parent | 3744c3b205e99d8d14b16973d3f17b68fc95b7cc (diff) | |
| download | perlweeklychallenge-club-2232cab693b42b73fbf94508a6ec0da9c0fea906.tar.gz perlweeklychallenge-club-2232cab693b42b73fbf94508a6ec0da9c0fea906.tar.bz2 perlweeklychallenge-club-2232cab693b42b73fbf94508a6ec0da9c0fea906.zip | |
- Added Perl solution to the task "Fibonacci Digit Sum" of week 149.
Diffstat (limited to 'challenge-149/mohammad-anwar/perl')
| -rw-r--r-- | challenge-149/mohammad-anwar/perl/ch-1.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-149/mohammad-anwar/perl/ch-1.pl b/challenge-149/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..a03c902a85 --- /dev/null +++ b/challenge-149/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +=head1 + +Week 149: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-149 + +Task #1: Fibonacci Digit Sum + + Given an input $N, generate the first $N numbers for which the sum of their digits is a Fibonacci number. + +=cut + +use strict; +use warnings; +use Test::More; + +is_deeply( + fibonacci_digit_sum(20), + [0, 1, 2, 3, 5, 8, 10, 11, 12, 14, 17, 20, 21, 23, 26, 30, 32, 35, 41, 44], + 'example' +); + +done_testing; + +# +# +# METHOD + +sub fibonacci_digit_sum { + my ($count) = @_; + + my @fibonacci = (0, 1); + my @digit_sum = @fibonacci; + my $index = 2; + while (@digit_sum < $count) { + while ($fibonacci[-1] <= $index) { + push @fibonacci, $fibonacci[-1] + $fibonacci[-2]; + } + + if ($index < 10) { + if (grep /$index/, @fibonacci) { + push @digit_sum, $index; + } + } + else { + my $sum = 0; + $sum += $_ for (split //,$index); + + if (grep /\b$sum\b/, @fibonacci) { + push @digit_sum, $index; + } + } + $index++; + } + + return \@digit_sum; +} |
