diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-01-24 15:33:40 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-01-24 15:33:40 +0100 |
| commit | 711d5679ff878c272bfd421116253630db330fc7 (patch) | |
| tree | 9588df06094a28aa73ade02c2b67262132e59da3 | |
| parent | 6699a62199e5dc264944ee891832f705048b9036 (diff) | |
| download | perlweeklychallenge-club-711d5679ff878c272bfd421116253630db330fc7.tar.gz perlweeklychallenge-club-711d5679ff878c272bfd421116253630db330fc7.tar.bz2 perlweeklychallenge-club-711d5679ff878c272bfd421116253630db330fc7.zip | |
Week 149, part 1: Perl solution
| -rw-r--r-- | challenge-149/abigail/perl/ch-1.pl | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-149/abigail/perl/ch-1.pl b/challenge-149/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..d1a1f7edb5 --- /dev/null +++ b/challenge-149/abigail/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-149 +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# This is sequence A028840 of the OEIS. The first 10,000 entries can +# be found at https://oeis.org/A028840/b028840.txt +# +# We will make use of the following conjecture: the Nth number is <= 10 * N. +# This holds for the first 10,000 numbers. +# +# Consider that we are asked to generate all the first N numbers (and not +# the Nth number), I doubt we'll be feeding numbers exceeding 10,000 to +# this program. +# +# For the sum of digits, we hark back to challenge 133, part 2. +# + +use List::Util qw [sum]; +sub digitsum ($n) {sum $n =~ /\d/ag} + +while (<>) { + my $N = 0 + $_; + # + # Get an upper bound on the sum of the digits. + # + my $max_fib = 9 * (1 + length $N); + # + # Generate all the Fibonacci numbers up to $max_fib. + # + my %fib = (0 => 1, 1 => 1); + my ($f, $g) = (0, 1); + while ($g < $max_fib) { + ($f, $g) = ($g, $f + $g); + $fib {$g} = 1; + } + + for (my ($c, $k) = (0, 0); $c < $N; $k ++) { + if ($fib {digitsum $k}) { + print "$k "; + $c ++; + } + } + print "\n"; +} |
