aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-25 20:20:31 +0100
committerGitHub <noreply@github.com>2022-10-25 20:20:31 +0100
commite24347426a9c6b1da1f581fc22ecbc63348d9fc1 (patch)
treed5f63e896a123c74cac7d41e632fa635afc4d149
parent547d8f94219b5bf23c24f359806c3c4680878220 (diff)
parent50b08241503a29dfc16335429344cce741bd6642 (diff)
downloadperlweeklychallenge-club-e24347426a9c6b1da1f581fc22ecbc63348d9fc1.tar.gz
perlweeklychallenge-club-e24347426a9c6b1da1f581fc22ecbc63348d9fc1.tar.bz2
perlweeklychallenge-club-e24347426a9c6b1da1f581fc22ecbc63348d9fc1.zip
Merge pull request #6965 from wlmb/challenges
Solve PWC 188
-rw-r--r--challenge-188/wlmb/blog.txt1
-rwxr-xr-xchallenge-188/wlmb/perl/ch-1.pl21
-rwxr-xr-xchallenge-188/wlmb/perl/ch-2.pl22
3 files changed, 44 insertions, 0 deletions
diff --git a/challenge-188/wlmb/blog.txt b/challenge-188/wlmb/blog.txt
new file mode 100644
index 0000000000..9458cb0342
--- /dev/null
+++ b/challenge-188/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io//2022/10/25/PWC188.org
diff --git a/challenge-188/wlmb/perl/ch-1.pl b/challenge-188/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..2ec087eec5
--- /dev/null
+++ b/challenge-188/wlmb/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 188
+# Task 1: Divisible Pairs
+#
+# See https://wlmb.github.io/2022/10/25/PWC188/#task-1-divisible-pairs
+use v5.36;
+use Algorithm::Combinatorics qw(combinations);
+die <<EOF unless @ARGV>=3;
+Usage: $0 D N1 N2 [N3...]\nto count distinct ordered pairs of integers Ni
+that add to a multiple of D
+EOF
+my $divisor=shift;
+my @list=sort @ARGV;
+my $iter=combinations(\@list, 2);
+my $count=0;
+while(my $z=$iter->next){
+ my ($x, $y)=@$z;
+ ++$count if ($x+$y)%$divisor==0;
+}
+say "The number of pairs from the list ",
+ join(", ", @ARGV), " with sum divisible by $divisor is $count";
diff --git a/challenge-188/wlmb/perl/ch-2.pl b/challenge-188/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..eb4d38e452
--- /dev/null
+++ b/challenge-188/wlmb/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 188
+# Task 2: Total Zero
+#
+# See https://wlmb.github.io/2022/10/25/PWC188/#task-2-total-zero
+use v5.36;
+use POSIX qw(floor);
+die <<EOF unless @ARGV==2;
+Usage: $0 x y
+To count operations required to bring x or y to zero by succesive subtraction.
+EOF
+my ($counter, $actual)=(0, 0);
+my ($x, $y)=my($x0, $y0)=@ARGV;
+die "Only positive integers allowed" unless $x>0 and $y>0 and $x==floor $x and $y==floor $y;
+use integer;
+while($y>0){
+ my ($d, $r)=($x/$y, $x%$y);
+ $counter += $d;
+ ++$actual;
+ ($x, $y)=($y, $r);
+}
+say "The number of operations required to bring $x0 $y0 to zero is $counter (divisions: $actual)";