aboutsummaryrefslogtreecommitdiff
path: root/challenge-164
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2022-05-14 17:04:32 -0600
committerRyan Thompson <i@ry.ca>2022-05-14 17:04:32 -0600
commit49e3407e960da4b2c204a9a63a43e341cc1313fb (patch)
tree2212cfd68b81ac80f7163fbe9e2c9a78a307e3a5 /challenge-164
parent1ab2f682f0ebf89bc859a3af176e4d5d045b0f7a (diff)
downloadperlweeklychallenge-club-49e3407e960da4b2c204a9a63a43e341cc1313fb.tar.gz
perlweeklychallenge-club-49e3407e960da4b2c204a9a63a43e341cc1313fb.tar.bz2
perlweeklychallenge-club-49e3407e960da4b2c204a9a63a43e341cc1313fb.zip
rjt's Week #164 solutions and blogs
Diffstat (limited to 'challenge-164')
-rw-r--r--challenge-164/ryan-thompson/README.md11
-rwxr-xr-xchallenge-164/ryan-thompson/perl/ch-1.pl31
-rwxr-xr-xchallenge-164/ryan-thompson/perl/ch-2.pl33
3 files changed, 69 insertions, 6 deletions
diff --git a/challenge-164/ryan-thompson/README.md b/challenge-164/ryan-thompson/README.md
index 6ee29833a6..d106e13922 100644
--- a/challenge-164/ryan-thompson/README.md
+++ b/challenge-164/ryan-thompson/README.md
@@ -1,17 +1,16 @@
# Ryan Thompson
-## Week 163 Solutions
+## Week 164 Solutions
-### Task 1 › Bitwise Sum
+### Task 1 › Palindromic Primes
* [Perl](perl/ch-1.pl)
- * [Raku](raku/ch-1.raku)
-### Task 2 › Summations
+### Task 2 › Happy Numbers
* [Perl](perl/ch-2.pl)
- * [Raku](raku/ch-2.raku)
## Blogs
- * [A Tail of Two Sums](https://ry.ca/2022/05/a-tail-of-two-sums/)
+ * [Palindromic Primes](https://ry.ca/2022/05/palindromic-primes/)
+ * [Happy Numbers](https://ry.ca/2022/05/happy-numbers/)
diff --git a/challenge-164/ryan-thompson/perl/ch-1.pl b/challenge-164/ryan-thompson/perl/ch-1.pl
new file mode 100755
index 0000000000..cb518cb71b
--- /dev/null
+++ b/challenge-164/ryan-thompson/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - Prime palindrome
+#
+# Even though there are 1.6x more primes than palindromes <= 1000,
+# we're already sieving the list, so we may as well use that as our
+# main loop. This is 5x faster than going the other way around.
+#
+# Same applies if we use Math::Prime::Util instead.
+#
+# 2022 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+
+no warnings 'uninitialized';
+
+say for grep { $_ eq scalar reverse $_ } primes_under( pop // 1000 );
+
+sub primes_under {
+ my $limit = shift;
+ my @comp; # Composite numbers (non-primes)
+
+ for my $n (2..$limit) {
+ next if $comp[$n];
+ $comp[$_] = 1 for map { $n * $_ } 2..$limit/$n;
+ }
+
+ 2, grep { !$comp[$_] } 3..$limit;
+}
diff --git a/challenge-164/ryan-thompson/perl/ch-2.pl b/challenge-164/ryan-thompson/perl/ch-2.pl
new file mode 100755
index 0000000000..904ef0bdf0
--- /dev/null
+++ b/challenge-164/ryan-thompson/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+#
+# ch-2.pl - Happy Numbers
+#
+# 2022 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+use List::Util qw< sum >;
+
+sub is_happy(_);
+
+my $count = pop // 8;
+
+# Output the first $count Happy Numbers
+for (local $_ = 1; $count > 0 ; $_++) {
+ next unless is_happy;
+ say;
+ $count--;
+}
+
+# Return true if $_ is a happy number
+sub is_happy(_) {
+ my $n = shift;
+
+ my %seen;
+ for (my $c = $n; $c != 1; $c = sum map { $_*$_ } split //, $c) {
+ return if $seen{$c}++;
+ }
+
+ return 1
+}