aboutsummaryrefslogtreecommitdiff
path: root/challenge-147
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-17 00:43:21 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-17 00:43:21 +0000
commitc390d06e3758e4f9f78c161221d9b0f296bdb5b5 (patch)
tree86af5c1e0a66058c5f8a0934e58adf2dedd3c05f /challenge-147
parent7e0ae81350424b9ee9ec81c38aa4adffded0817e (diff)
downloadperlweeklychallenge-club-c390d06e3758e4f9f78c161221d9b0f296bdb5b5.tar.gz
perlweeklychallenge-club-c390d06e3758e4f9f78c161221d9b0f296bdb5b5.tar.bz2
perlweeklychallenge-club-c390d06e3758e4f9f78c161221d9b0f296bdb5b5.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-147')
-rwxr-xr-xchallenge-147/colin-crain/perl/ch-1.pl142
-rwxr-xr-xchallenge-147/colin-crain/perl/ch-2.pl131
2 files changed, 273 insertions, 0 deletions
diff --git a/challenge-147/colin-crain/perl/ch-1.pl b/challenge-147/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..b826de7e10
--- /dev/null
+++ b/challenge-147/colin-crain/perl/ch-1.pl
@@ -0,0 +1,142 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# prime-rime-ime-me-e.pl
+#
+# Truncatable Prime
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to generate first 20 left-truncatable prime
+# numbers in base 10.
+#
+# In number theory, a left-truncatable prime is a prime number
+# which, in a given base, contains no 0, and if the leading left
+# digit is successively removed, then all resulting numbers are
+# primes.
+#
+# Example
+#
+# 9137 is one such left-truncatable prime since 9137, 137, 37
+# and 7 are all prime numbers.
+
+# analysis:
+#
+# Number Theory is so weird. Take two ideas, as tenuously
+# related as any you can come up with, and weld them together,
+# then look and see what Frankenstein's monster you've created,
+# just to see if anything can be inferred from the data.
+#
+# Which itself is likely to be another question, another
+# derivitive in search of a place in a greater fabric of
+# meaning.
+#
+# There's a certain devil-may-care undercurrent to it all,
+# consequence be damned: With my new rabbit-fish soldiers my
+# army will be unstoppable! I hold the powers of life and death
+# itself in my hands!
+#
+# The mad-scientist metaphor is particularly apropos as the
+# patterns detected will be nearly by definition unobvious.
+# After all, if it were obvious it would probably just be
+# absorbed into some sort of more mainstream mathematics. What
+# is being looked for in Number Theory are the deep
+# connections, tendrils left over from the creation of the
+# universe itself, or even before and beyond the creation.
+# After all, math exists, metaphysically, outside the universe.
+#
+# Or perhaps that premise too can be disputed. Nothing is off
+# the table. We are investigating the hairy edge-cases of
+# reality with a ferver that aligns with the mystical. Newton,
+# it is known now, was an alchemist.
+#
+# "There are more things in heaven and earth, Horatio,
+# Than are dreamt of in your philosophy."
+# — Hamlet
+#
+# Why would anyone do this? It's obvious: because it's there,
+# of course. If you could stare into the center of creation,
+# why wouldn't you?
+#
+# Bring sunglasses.
+#
+# method:
+#
+# As we have no idea of how many primes we will require, we'll
+# rework our prime generator as an iterator, maintaining an
+# internal list, computing and returning the next prime,
+# whatever that is, when called. These primes are then placed
+# in a hash for easy lookup, as we'll need to consult our list
+# of already-computed primes often, with random access.
+#
+# When we have a new prime, we hand it to a truncatable()
+# subroutine to see how it fares. Inside a loop the leftmost
+# digit is lopped off using substr() and the value is
+# rechecked. If at any time the lopped number is no longer
+# prime 0 is returned, and if it successfully runs the gauntlet
+# we return 1. This subroutine is used for a loop conditional
+# to try the next candidate prime. If the test is passed we add
+# the candidate to the list of left-trancatables and continue.
+#
+# We'll skip the trivial cases of single-digit primes as
+# uninteresting, and because our algorithm runs quickly we'll
+# compute a thousand instead of 20.
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+sub get_next_prime ( ) {
+## an iterator that delivers the next prime
+ state @primes;
+
+ if ( @primes < 2 ) {
+ push @primes, @primes == 0 ? 2 : 3;
+ return $primes[-1];
+ }
+
+ my $candidate = $primes[-1] ;
+ CANDIDATE: while ( $candidate += 2 ) {
+ my $sqrt_candidate = sqrt( $candidate );
+ for my $test ( @primes ) {
+ next CANDIDATE if $candidate % $test == 0;
+ last if $test > $sqrt_candidate;
+ }
+ push @primes, $candidate;
+ return $candidate;
+ }
+}
+
+my $prime_lookup;
+my @lt_primes;
+
+while ( @lt_primes <= 1000 ) {
+ my $candidate = get_next_prime();
+ $prime_lookup->{ $candidate } = 1;
+ next unless left_truncatable( $candidate, $prime_lookup );
+ $candidate and push @lt_primes, $candidate;
+}
+
+say $_ for @lt_primes;
+
+sub left_truncatable ( $val, $lookup ) {
+ return 0 if $val =~ /0/;
+ return 0 if $val =~ /^\d$/;
+
+ while ( 1 ) {
+ substr $val, 0, 1, '';
+ last unless $val;
+ return 0 unless $lookup->{$val};
+ }
+ return 1;
+}
+
+
diff --git a/challenge-147/colin-crain/perl/ch-2.pl b/challenge-147/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..87809817ab
--- /dev/null
+++ b/challenge-147/colin-crain/perl/ch-2.pl
@@ -0,0 +1,131 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# .pl
+#
+# Pentagon Numbers
+# Submitted by: Mohammad S Anwar
+# Write a sript to find the first pair of Pentagon Numbers whose
+# sum and difference are also a Pentagon Number.
+#
+# Pentagon numbers can be defined as P(n) = n(3n - 1)/2.
+#
+# Example
+# The first 10 Pentagon Numbers are:
+# 1, 5, 12, 22, 35, 51, 70, 92, 117 and 145.
+#
+# P(4) + P(7) = 22 + 70 = 92 = P(8)
+# but
+# P(4) - P(7) = |22 - 70| = 48 is not a Pentagon Number.
+#
+# method:
+#
+# Shooting from the hip, I saw this as a math problem: we have
+# three variables, that fit into two equations. It's a fairly
+# simple system of linear equations that should be solvable without
+# going crazy.
+#
+# But then again it's not a math problem, its a programming
+# problem. It's both, really, but sometimes things can be two
+# things at once. Trust me, I know: I had a girlfriend like that
+# once a long time ago, and I learned this lesson the hard way.
+#
+# But I digress.
+#
+# It's a little unclear what we mean my "first pair" in this
+# multidimensional context. I can think of a couple of criteria:
+# the lowest first value? Combined value? Summed? Multiplied?
+#
+# Although not stated in the description if we conclude that the
+# pentogonal numbers are a the next extension of square numbers and
+# triangular numbers before those, then the pentagonals describe a
+# cardinal quantity, and as such are positive or zero. And we
+# probably exclude zero.
+#
+# This is indeed the case, and we should add `n ≥ 1` to the above
+# description.
+#
+# From the quadratic definition we can next see that the
+# pentagonals will always be increasing, P(n+1) > P(n) ∀ n ≥ 1
+#
+# So from this we can require that for two values n and m, such
+# that
+#
+# P(n) - P(m) = P(t)
+#
+# for some t, then m must be less than n.
+#
+# We'll start with the tuple
+#
+# (n,m) = (2,1)
+#
+# and start working upwards from there, incrementing n by 1 and
+# then searching m for all values 1 to n-1.
+#
+# This searches the data space in a regular, expanding pattern that
+# should satisfy some definitions of "first".
+#
+# It's brutal but should be effective. We'll quit when we find one
+# or stop the process because we're tired of waiting.
+#
+# Report:
+#
+# If finds the solution, n = 2167, m = 1020, in short order:
+#
+# found pair n = 2167, m = 1020 :
+#
+# P(2167) = 7042750
+# P(1020) = 1560090
+# sum is 8602840
+# which is P(2395)
+# diff is 5482660
+# which is P(1912)
+#
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+my %penta = map { $_ => pentagons($_) } (1..1000000 );
+my %lookup = reverse %penta;
+my ($n, $m) = (1,0);
+
+OUTER: while ( $n++ ) {
+ $m = 0;
+ while ( ++$m < $n ) {
+ last OUTER if exists $lookup{ $penta{$n} + $penta{$m} } and
+ exists $lookup{ $penta{$n} - $penta{$m} } ;
+ }
+}
+
+sub pentagons ( $n ) {
+ return $n * ( 3 * $n - 1 ) / 2
+}
+
+## output report
+
+my $sum = $penta{$n} + $penta{$m};
+my $diff = $penta{$n} - $penta{$m};
+
+say <<~"END";
+ found pair n = $n, m = $m :
+
+ P($n) = $penta{$n}
+ P($m) = $penta{$m}
+ sum is $sum
+ which is P($lookup{$sum})
+ diff is $diff
+ which is P($lookup{$diff})
+ END
+
+