diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-17 09:51:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-17 09:51:23 +0100 |
| commit | 7a5d8aabaff4a93dbeaf596b54c4755cfb9d865d (patch) | |
| tree | f59a55e4a378f42fa80a5b6f13238b12088e7d4b | |
| parent | 7c2fa584618da574d42b1713c3c343423991c781 (diff) | |
| parent | c92803076d8d8f9fee7bb945e0e268399e32cdd3 (diff) | |
| download | perlweeklychallenge-club-7a5d8aabaff4a93dbeaf596b54c4755cfb9d865d.tar.gz perlweeklychallenge-club-7a5d8aabaff4a93dbeaf596b54c4755cfb9d865d.tar.bz2 perlweeklychallenge-club-7a5d8aabaff4a93dbeaf596b54c4755cfb9d865d.zip | |
Merge pull request #6275 from rjt-pl/master
rjt's week 169 solutions and blog
| -rw-r--r-- | challenge-169/ryan-thompson/README.md | 11 | ||||
| -rw-r--r-- | challenge-169/ryan-thompson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-169/ryan-thompson/perl/ch-1.pl | 41 | ||||
| -rwxr-xr-x | challenge-169/ryan-thompson/perl/ch-2.pl | 21 |
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]; +} |
