aboutsummaryrefslogtreecommitdiff
path: root/challenge-009
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-12 23:13:15 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-23 18:03:40 +0100
commitdb49df92128aa2a44b89e6341c5096e4f579c2d2 (patch)
tree5dbf63493f24abe7f3a58263f1ca61e795df380b /challenge-009
parentbdb872733ea2328d22df0bf5ebcfcbad04cd84da (diff)
downloadperlweeklychallenge-club-db49df92128aa2a44b89e6341c5096e4f579c2d2.tar.gz
perlweeklychallenge-club-db49df92128aa2a44b89e6341c5096e4f579c2d2.tar.bz2
perlweeklychallenge-club-db49df92128aa2a44b89e6341c5096e4f579c2d2.zip
Challenge 009 task 1
Diffstat (limited to 'challenge-009')
-rwxr-xr-xchallenge-009/jo-37/perl/ch-1.pl64
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-009/jo-37/perl/ch-1.pl b/challenge-009/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..9c41c49ff0
--- /dev/null
+++ b/challenge-009/jo-37/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Gen;
+use Math::Prime::Util 'todigits';
+use List::Util 'uniq';
+use experimental 'signatures';
+
+our ($tests, $examples, $base);
+$base ||= 10;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-base=B
+ process numbers in base B
+
+N
+ find the first square having at least N different digits in base B.
+
+EOS
+
+
+### Input and Output
+
+say gen_squares(shift, $base)->();
+
+### Implementation
+
+
+# Build a generator for square numbers having at least $n different
+# digits in base $base.
+sub gen_squares ($n, $base) {
+ <1..>->map('**2')->filter(sub{uniq(todigits($_, $base)) >= $n});
+}
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is gen_squares(5, 10)->take(20),
+ [12769, 13456, 13689, 13924, 15376, 15876, 16384, 17689, 17956,
+ 18496, 18769, 20164, 20736, 21609, 21904, 23104, 23409, 23716,
+ 28561, 29584], 'from A235720';
+ }
+
+ done_testing;
+ exit;
+}