aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2022-03-14 14:06:05 -0600
committerLuis Mochan <mochan@fis.unam.mx>2022-03-14 14:06:05 -0600
commit43e9b02f5439cddcb683aa4c1ef4d84b7238f7a9 (patch)
tree69757f7e19df5b175b1c6495a3c610bd89f45fdd
parentafe15d7cb1ff0c3d30fa1c8dd81a554f30c3b4df (diff)
downloadperlweeklychallenge-club-43e9b02f5439cddcb683aa4c1ef4d84b7238f7a9.tar.gz
perlweeklychallenge-club-43e9b02f5439cddcb683aa4c1ef4d84b7238f7a9.tar.bz2
perlweeklychallenge-club-43e9b02f5439cddcb683aa4c1ef4d84b7238f7a9.zip
Solve PWC 156
-rw-r--r--challenge-156/wlmb/blog.txt1
-rwxr-xr-xchallenge-156/wlmb/perl/ch-1.pl19
-rwxr-xr-xchallenge-156/wlmb/perl/ch-2.pl27
3 files changed, 47 insertions, 0 deletions
diff --git a/challenge-156/wlmb/blog.txt b/challenge-156/wlmb/blog.txt
new file mode 100644
index 0000000000..9dac6f78ab
--- /dev/null
+++ b/challenge-156/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2022/03/14/PWC156/
diff --git a/challenge-156/wlmb/perl/ch-1.pl b/challenge-156/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..5e377911de
--- /dev/null
+++ b/challenge-156/wlmb/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 156
+# Task 1: Pernicious numbers
+#
+# See https://wlmb.github.io/2022/03/14/PWC156/#task-1-pernicious-numbers
+use v5.12;
+use warnings;
+use bigint;
+use Math::Prime::Util qw(is_prime);
+use List::Util qw(sum0);
+my $N=shift//10; # How many pernicious numbers to calculate
+my $candidate=0;
+my @pernicious;
+for(1..$N){
+ push(@pernicious, $candidate), next
+ if is_prime(sum0 split "", sprintf "%b", ++$candidate);
+ redo
+}
+say "The first $N pernicious numbers are ", join ", ", @pernicious;
diff --git a/challenge-156/wlmb/perl/ch-2.pl b/challenge-156/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..f874b52464
--- /dev/null
+++ b/challenge-156/wlmb/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 156
+# Task 2: Weird number
+#
+# See https://wlmb.github.io/2022/03/14/PWC156/#task-2-weird-number
+use v5.12;
+use warnings;
+use Math::Prime::Util qw(divisors);
+use Algorithm::Combinatorics qw(subsets);
+use List::Util qw(sum0);
+die "Usage: ./ch-2.pl N1 N2... to test numbers N1, N2... for weirdness"
+ unless @ARGV;
+my $is_weird;
+ WEIRD:
+ for my $N(@ARGV){
+ say("Arguments must be larger than 1"), next unless $N>=2;
+ my @divisors=divisors($N);
+ pop @divisors; # keep only proper divisors
+ $is_weird=0, next WEIRD unless sum0(@divisors)>$N; # Overabundant?
+ my @subsets=subsets(\@divisors);
+ for(@subsets){
+ $is_weird=0, next WEIRD if sum0(@$_)==$N; #Semiperfect, fail
+ }
+ $is_weird=1;
+} continue {
+ say "$N ", $is_weird?"is":"is not", " weird";
+}