aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-01 18:36:39 +0000
committerGitHub <noreply@github.com>2021-12-01 18:36:39 +0000
commit561fa469408d0ff8846cf7827b7991bbf657bd29 (patch)
tree877f60c5756d3e3b311be15a5db1ed94b84ab819
parentcf512a51aa532b5c9bdbf916073887abbc70952f (diff)
parent351adc907c30510c93e4dae7e36dd168d227ab47 (diff)
downloadperlweeklychallenge-club-561fa469408d0ff8846cf7827b7991bbf657bd29.tar.gz
perlweeklychallenge-club-561fa469408d0ff8846cf7827b7991bbf657bd29.tar.bz2
perlweeklychallenge-club-561fa469408d0ff8846cf7827b7991bbf657bd29.zip
Merge pull request #5311 from wlmb/challenges
Solve PWC141
-rw-r--r--challenge-141/wlmb/blog.txt1
-rwxr-xr-xchallenge-141/wlmb/perl/ch-1.pl21
-rwxr-xr-xchallenge-141/wlmb/perl/ch-2.pl24
3 files changed, 46 insertions, 0 deletions
diff --git a/challenge-141/wlmb/blog.txt b/challenge-141/wlmb/blog.txt
new file mode 100644
index 0000000000..d0ece13f8f
--- /dev/null
+++ b/challenge-141/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2021/11/29/PWC141/
diff --git a/challenge-141/wlmb/perl/ch-1.pl b/challenge-141/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..1eb780b7d7
--- /dev/null
+++ b/challenge-141/wlmb/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 141
+# Task 1: Number divisors
+#
+# See https://wlmb.github.io/2021/11/29/PWC141/#task-1-number-divisors
+use v5.12;
+use warnings;
+use PDL;
+use PDL::NiceSlice;
+#Set defaults and params. from com. line
+my %params=(try=>100, divisors=>8, results=>10, @ARGV);
+my ($try, $divisors, $results)
+ =@params{qw(try divisors results)};
+my $cells=zeroes $try;
+# count divisors for each number
+$cells($_:-1:$_)+=1 for(1..$try-1);
+# find all d-multiples
+my $multiples=$cells->xvals->where($cells==$divisors);
+die "Need to increase try" unless $multiples->nelem>=$results;
+my $out=$multiples(0:$results-1);
+say "try=$try, divisors=$divisors, results=$results, out=$out";
diff --git a/challenge-141/wlmb/perl/ch-2.pl b/challenge-141/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..03f3c416e1
--- /dev/null
+++ b/challenge-141/wlmb/perl/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 141
+# Task 2: Like numbers
+#
+# See https://wlmb.github.io/2021/11/29/PWC141/#task-2-like-numbers
+use v5.12;
+use warnings;
+use PDL;
+use PDL::NiceSlice;
+
+die "Usage: ./ch-2.pl m n" unless @ARGV==2;
+my ($m,$n)=@ARGV;
+my $l=length $m;
+my $digits=pdl[split "", $m];
+my $multiples= pdl [
+ sort {$a<=>$b}
+ grep {$_!~/^0/&&$_%$n==0}
+ map {
+ my $bits=pdl([split "", sprintf "%b", $_]);
+ join "", $digits->where($bits->(-1:0))->list;
+ } 1..2**$l-2
+ ];
+say "m=$m, n=$n, Out=", $multiples->nelem,
+ "\nas multiples=$multiples";