aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2022-06-16 17:09:16 -0600
committerRyan Thompson <i@ry.ca>2022-06-16 17:09:16 -0600
commit40198d275c123f30a185aea4da69f2c3b9424dab (patch)
tree9f6a57afcf51805df2808d3a8e35c6ca9062fea6
parent8810e3636cb9c2a1b553481bf3b4ae1fc3841784 (diff)
downloadperlweeklychallenge-club-40198d275c123f30a185aea4da69f2c3b9424dab.tar.gz
perlweeklychallenge-club-40198d275c123f30a185aea4da69f2c3b9424dab.tar.bz2
perlweeklychallenge-club-40198d275c123f30a185aea4da69f2c3b9424dab.zip
rjt's week 169 solutions and blog
-rw-r--r--challenge-169/ryan-thompson/README.md11
-rw-r--r--challenge-169/ryan-thompson/blog.txt1
-rwxr-xr-xchallenge-169/ryan-thompson/perl/ch-1.pl41
-rwxr-xr-xchallenge-169/ryan-thompson/perl/ch-2.pl21
4 files changed, 67 insertions, 7 deletions
diff --git a/challenge-169/ryan-thompson/README.md b/challenge-169/ryan-thompson/README.md
index fb5a53ccbd..eb6576b9e4 100644
--- a/challenge-169/ryan-thompson/README.md
+++ b/challenge-169/ryan-thompson/README.md
@@ -1,18 +1,15 @@
# Ryan Thompson
-## Week 168 Solutions
+## Week 169 Solutions
-### Task 1 › Perrin Primes
+### Task 1 › Brilliant Numbers
* [Perl](perl/ch-1.pl)
- * [Raku](raku/ch-1.raku)
-### Task 2 › Home Prime
+### Task 2 › Achilles Numbers
* [Perl](perl/ch-2.pl)
- * [Raku](raku/ch-2.raku)
## Blogs
- * [Perrin Primes](https://ry.ca/2022/06/perrin-primes/)
- * [Home Prime](https://ry.ca/2022/06/home-prime/)
+ * [Brilliant and Achilles Numbers](https://ry.ca/2022/06/brilliant-and-achilles-numbers/)
diff --git a/challenge-169/ryan-thompson/blog.txt b/challenge-169/ryan-thompson/blog.txt
new file mode 100644
index 0000000000..2da3a4c28e
--- /dev/null
+++ b/challenge-169/ryan-thompson/blog.txt
@@ -0,0 +1 @@
+https://ry.ca/2022/06/brilliant-and-achilles-numbers/
diff --git a/challenge-169/ryan-thompson/perl/ch-1.pl b/challenge-169/ryan-thompson/perl/ch-1.pl
new file mode 100755
index 0000000000..9594534ecf
--- /dev/null
+++ b/challenge-169/ryan-thompson/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - 2-Brilliant numbers
+#
+# 2022 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+
+say for brilliant($ARGV[0] // 20);
+
+# Return the first $count 2-Brilliant numbers
+# This works like a prime sieve, but for every power of 10, we
+# add another step of combining factors to make brilliant numbers
+sub brilliant {
+ my $count = $_[0];
+
+ my @comp = (undef, 1, undef); # Composite numbers
+ my @brilliant; # Results
+
+ for (my $lim = 10; $count > 0; $lim .= '0') {
+ # Partial sieve
+ for my $n (2..$lim) {
+ next if $n > 2 and $comp[$n];
+ $comp[$_] = $_ for map { $n * $_ } 2..$lim/$n;
+ }
+
+ # Pull out prime factors between $lim/10 and $lim
+ my @fac = grep { !$comp[$_] } ($lim/10)..$lim;
+
+ # Map all unique 2-permutations of prime factors $lim/10..$lim
+ while (my ($i1, $n1) = each @fac) {
+ push @brilliant, $n1 * $_ for @fac[$i1..$#fac];
+ $count -= @fac - $i1;
+ }
+ }
+
+ (sort { $a <=> $b } @brilliant)[0..$_[0]-1]
+}
diff --git a/challenge-169/ryan-thompson/perl/ch-2.pl b/challenge-169/ryan-thompson/perl/ch-2.pl
new file mode 100755
index 0000000000..4614b2d5a2
--- /dev/null
+++ b/challenge-169/ryan-thompson/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+#
+# ch-2.pl - Achilles numbers
+#
+# 2022 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+
+use Math::Prime::Util qw< gcd factor_exp >;
+use List::Util qw< all any >;
+
+say for achilles($ARGV[0] // 5000);
+
+sub achilles {
+ map { $_->[0] }
+ grep { all { $_ > 1 } @{$_->[1]} and gcd(@{$_->[1]}) == 1 }
+ map { [ $_ => [ map { $_->[1] } factor_exp($_) ] ] } 2..$_[0];
+}