aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-06-13 16:54:14 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2022-06-13 16:54:14 +0100
commit496d9e82f4c504ee2eeeaba3053c95e9c7f9dc58 (patch)
treecfe9904df3a4efd8592ad39d07f0f1cf8dbeaf0a
parent8810e3636cb9c2a1b553481bf3b4ae1fc3841784 (diff)
downloadperlweeklychallenge-club-496d9e82f4c504ee2eeeaba3053c95e9c7f9dc58.tar.gz
perlweeklychallenge-club-496d9e82f4c504ee2eeeaba3053c95e9c7f9dc58.tar.bz2
perlweeklychallenge-club-496d9e82f4c504ee2eeeaba3053c95e9c7f9dc58.zip
Challenge week 169
-rw-r--r--challenge-169/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-169/peter-campbell-smith/perl/ch-1.pl32
-rwxr-xr-xchallenge-169/peter-campbell-smith/perl/ch-2.pl62
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-169/peter-campbell-smith/blog.txt b/challenge-169/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..a5b4b51fb7
--- /dev/null
+++ b/challenge-169/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/06/brilliant-and-achilles.html
diff --git a/challenge-169/peter-campbell-smith/perl/ch-1.pl b/challenge-169/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..707f322b37
--- /dev/null
+++ b/challenge-169/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-06-13
+# PWC 169 task 1
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+use Math::Prime::Util qw[factor];
+
+# Write a script to generate first 20 Brilliant Numbers, which are numbers having just two prime factors,
+# and those are the same length.
+
+# Blog at https://pjcs-pwc.blogspot.com/2022/06/brilliant-and-achilles.html
+
+my ($test, @pf, $result, $count);
+
+# loop till done
+for ($test = 1, $count = 0; $count < 20; $test ++) {
+ @pf = factor($test);
+
+ # test for brilliance
+ next unless (scalar @pf == 2 and length($pf[0]) == length($pf[1]));
+
+ # accumulate answers
+ $result .= qq[$test, ];
+ $count ++;
+}
+
+# tell the world
+say substr($result, 0, -2);
diff --git a/challenge-169/peter-campbell-smith/perl/ch-2.pl b/challenge-169/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..2ea8b87152
--- /dev/null
+++ b/challenge-169/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-06-13
+# PWC 169 task 2
+
+use v5.28;
+use strict;
+use warnings;
+use utf8;
+use Math::Prime::Util qw[factor];
+
+# Write a script to generate first 20 Achilles Numbers: numbers that are powerful but imperfect. A positive integer
+# n is a powerful number if, for every prime factor p of n, p^2 is also a divisor. A number is imperfect if it has
+# at least two distinct prime factors.
+
+# Blog at https://pjcs-pwc.blogspot.com/2022/06/brilliant-and-achilles.html
+
+my ($test, @pf, $f, $result, $count, $dpf, %seen, $p, $rarest_factor, $good);
+
+# loop from 1 up until done
+TEST: for ($test = 1, $count = 0; $count < 20; $test ++) {
+
+ # get prime factors
+ @pf = factor($test);
+
+ # test for powerfulness
+ $dpf = 0;
+ %seen = ();
+ for $f (@pf) {
+
+ # discard unless $test is a multiple of prime factor $f squared
+ next TEST unless $test % ($f ** 2) == 0;
+
+ # count distinct prime factors and their frequency
+ $dpf ++ unless $seen{$f};
+ $seen{$f} ++;
+ }
+
+ # test for imperfection
+ next unless $dpf >= 2;
+
+ # test for not a perfect power: first find the least frequently repeating prime factor
+ $rarest_factor = ~0;
+ for $f (keys %seen) {
+ last if $seen{$f} == 1;
+ $rarest_factor = $seen{$f} if $seen{$f} < $rarest_factor;
+ }
+
+ # then check that at least one other pf repeats a non-multiple of time the least frequent
+ $good = 0;
+ for $f (keys %seen) {
+ $good = 1 if $seen{$f} % $rarest_factor != 0;
+ }
+ next TEST unless $good;
+
+ # accumulate answers
+ $result .= qq[$test, ];
+ $count ++;
+}
+
+# tell the world
+say substr($result, 0, -2);