diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-28 07:45:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-28 07:45:16 +0000 |
| commit | 3097bb20c921d3fc43ca81a35446143e903f8e70 (patch) | |
| tree | a1d789f6356f49a22bfb586ca967d76c167321cd | |
| parent | 57e344b8755ee417dcb013a78dab80c527a75047 (diff) | |
| parent | 53eeafe7f1922b01684a8bbd7ac9eabda895c585 (diff) | |
| download | perlweeklychallenge-club-3097bb20c921d3fc43ca81a35446143e903f8e70.tar.gz perlweeklychallenge-club-3097bb20c921d3fc43ca81a35446143e903f8e70.tar.bz2 perlweeklychallenge-club-3097bb20c921d3fc43ca81a35446143e903f8e70.zip | |
Merge pull request #5576 from jo-37/contrib
Solutions to challenge 149
| -rwxr-xr-x | challenge-149/jo-37/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-149/jo-37/perl/ch-2.pl | 58 |
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-149/jo-37/perl/ch-1.pl b/challenge-149/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..ce0f8b0bcf --- /dev/null +++ b/challenge-149/jo-37/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Coro::Generator; +use Math::Prime::Util qw(lucasu todigits vecsum); + +our ($examples, $base); +$base ||= 10; + +run_tests() if $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-base=B] [N] + +-examples + run the examples from the challenge + +-base=B + consider numbers in base B. Default: 10 + +N + Print the first N numbers having a digit sum in base B that is a + Fibonacci number. + +EOS + + +### Input and Output + +{ + my $fib_digsum_seq = gen_fib_digsum_seq(); + say $fib_digsum_seq->() for 1 .. shift; +} + + +### Implementation + +sub gen_fib_digsum_seq { + # Considering only numbers with a digit sum less than 233. + state $fib = {map +(lucasu(1, -1, $_) => undef), 0 .. 12}; + + # Build a generator for numbers having a digit sum that is a + # Fibonacci number. + generator { + for (my $k = 0;; $k++) { + yield $k if exists $fib->{vecsum(todigits $k, $base)}; + } + } +} + + +### Examples and tests + +sub run_tests { + + my $fib_digsum_seq = gen_fib_digsum_seq(); + is [map $fib_digsum_seq->(), 1 .. 20], + [qw(0 1 2 3 5 8 10 11 12 14 17 20 21 23 26 30 32 35 41 44)], + 'example 1'; + + done_testing; + exit; +} diff --git a/challenge-149/jo-37/perl/ch-2.pl b/challenge-149/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..20cf301279 --- /dev/null +++ b/challenge-149/jo-37/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use List::AllUtils qw(frequency max pairvalues); +use Math::Prime::Util qw(fromdigits todigits todigitstring); +use experimental 'signatures'; + +our $examples; + +run_tests() if $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [base] + +-examples + run the examples from the challenge + +base + Find the largest perfect square having no repeated digits in the + given base. + +EOS + + +### Input and Output + +# Print the largest square in the requested base. +say todigitstring(max_square($ARGV[0]), $ARGV[0]); + + +### Implementation + +# The maximum number having no repeated digits in a given base is the +# number built from all digits in descending order. Trying all perfect +# squares down from this maximum. +# +# Returns an integer, not the requested string. +sub max_square ($base) { + for (my $r = int sqrt fromdigits [reverse 0 .. $base - 1], $base;; $r--) { + my $k = $r ** 2; + return $k if 1 == max pairvalues frequency todigits $k, $base; + } +} + + + +### Examples and tests + +sub run_tests { + is todigitstring(max_square(2), 2), '1', 'example 1'; + is todigitstring(max_square(4), 4), '3201', 'example 2'; + is max_square(10), 9814072356, 'example 3'; + like todigitstring(max_square(12), 12), qr/^B8750A649321$/i, 'example 4'; + + done_testing; + exit; +} |
