diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-29 12:28:28 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-29 12:28:28 +0000 |
| commit | dc6adce803a5d8affa26b0d58a4653866f0af722 (patch) | |
| tree | 058f4192b5ced30105f59b97305cf868cb1dc804 | |
| parent | 25f6592b0321823bc1326e9e8649eaaa16b01078 (diff) | |
| parent | 381b99fee0340d80e551135e0954bfb13f1b5383 (diff) | |
| download | perlweeklychallenge-club-dc6adce803a5d8affa26b0d58a4653866f0af722.tar.gz perlweeklychallenge-club-dc6adce803a5d8affa26b0d58a4653866f0af722.tar.bz2 perlweeklychallenge-club-dc6adce803a5d8affa26b0d58a4653866f0af722.zip | |
Merge pull request #5580 from LubosKolouch/master
Challenge 149 Task 1 Perl solution without using the Fib library
| -rw-r--r-- | challenge-149/lubos-kolouch/perl/ch-1_no_lib.pl | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-149/lubos-kolouch/perl/ch-1_no_lib.pl b/challenge-149/lubos-kolouch/perl/ch-1_no_lib.pl new file mode 100644 index 0000000000..3163c58105 --- /dev/null +++ b/challenge-149/lubos-kolouch/perl/ch-1_no_lib.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use List::Util qw/sum/; + +my @fibs; +my %fib_hash; + +$fibs[0] = 0; +$fibs[1] = 1; +$fib_hash{0} = 1; +$fib_hash{1} = 1; + +sub gen_more_fibs { + my $limit = shift; + + while ($fibs[-1] < $limit) { + my $new_fib =$fibs[-1] + $fibs[-2]; + push @fibs, $new_fib; + $fib_hash{$new_fib} = 1; + } + +} + +sub get_numbers { + my $what = shift; + + my $count = 0; + my $pos = 0; + + my @output; + + while($count < $what) { + gen_more_fibs($pos); + if ($fib_hash{sum(split //, $pos)}) { + push @output, $pos; + $count++; + } + $pos++; + + } + + return \@output; +} + +use Test::More; + +is_deeply(get_numbers(20), [0, 1, 2, 3, 5, 8, 10, 11, 12, 14, 17, 20, 21, 23, 26, 30, 32, 35, 41, 44]); +done_testing; |
