aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-06-12 09:32:40 +0100
committerGitHub <noreply@github.com>2022-06-12 09:32:40 +0100
commit70c97aefd1f045fd6dfe752d6ca346b8620ef1e1 (patch)
treebb71a5c7953446af6714d90dada74957e2c3a1e0
parent32841b4de5d3eec6ccf0a57f1e97ae94b0171d38 (diff)
parente66b82a635ca6306adf4b7d46e8b675159e47c8e (diff)
downloadperlweeklychallenge-club-70c97aefd1f045fd6dfe752d6ca346b8620ef1e1.tar.gz
perlweeklychallenge-club-70c97aefd1f045fd6dfe752d6ca346b8620ef1e1.tar.bz2
perlweeklychallenge-club-70c97aefd1f045fd6dfe752d6ca346b8620ef1e1.zip
Merge pull request #6242 from E7-87-83/newt
Week 168 Submission
-rw-r--r--challenge-168/cheok-yin-fung/perl/ch-1.pl29
-rw-r--r--challenge-168/cheok-yin-fung/perl/ch-2.pl72
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-168/cheok-yin-fung/perl/ch-1.pl b/challenge-168/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..faf90a9bb3
--- /dev/null
+++ b/challenge-168/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+# The Weekly Challenge 168
+# Task 1 Perrin Prime
+use v5.24.0;
+use warnings;
+use List::Util qw/none/;
+use Math::Prime::Util qw/is_prime/;
+
+my @perrin_primes = (2,3);
+my ($ppnm3,$ppnm2,$ppnm1, $ppn) = (3,0,2,3);
+
+while (scalar @perrin_primes < 13) {
+ if (is_prime($ppn) == 2) {
+ push @perrin_primes, $ppn if none {$_ == $ppn} @perrin_primes;
+ say $ppn;
+ }
+ $ppnm3 = $ppnm2;
+ $ppnm2 = $ppnm1;
+ $ppnm1 = $ppn;
+ $ppn = $ppnm2 + $ppnm3;
+}
+
+say join ", ", @perrin_primes;
+
+# time:
+# real 0m0.025s
+# user 0m0.017s
+# sys 0m0.009s
+
diff --git a/challenge-168/cheok-yin-fung/perl/ch-2.pl b/challenge-168/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..00406d25ef
--- /dev/null
+++ b/challenge-168/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+# The Weekly Challenge 168
+# Task 2 Home Prime
+# Usage: ch-2.pl [integer > 1]
+use v5.24.0;
+use warnings;
+use Math::Prime::Util qw/is_prime next_prime/;
+use Math::BigInt;
+
+say hp($ARGV[0]) if defined($ARGV[0]) && $ARGV[0] != 1;
+
+
+
+sub hp {
+ my_hp($_[0],0);
+}
+
+
+
+sub my_hp {
+ my $num = $_[0];
+ my $recur_depth = $_[1];
+ die "Number involved too large.\n"
+ if
+ Math::BigInt->new("18446744073709551616") # 2**64
+ ->ble( Math::BigInt->new($num) );
+
+
+ if (is_prime($num) == 2) {
+ return $num;
+ }
+
+ die "Walk so far but still cannot get result. :(\n" if $recur_depth > 20;
+
+ my @factors = ();
+ my $p = 2;
+ while ($num != 1) {
+ if (!($num % $p)) {
+ push @factors, $p;
+ $num /= $p;
+ }
+ else {
+ $p = next_prime($p);
+ }
+ if ($p > sqrt $num) {
+ say "useful";
+ push @factors, $num;
+ last;
+ }
+ }
+ my $nxt = join "", @factors;
+ say " $nxt";
+ return my_hp($nxt, ++$recur_depth);
+}
+
+use Test::More tests => 8;
+# test data from OEIS
+ok hp(10) == 773;
+ok hp(24) == 331_319;
+ok hp(32) == 241_271;
+ok hp(40) == 3314192745739;
+ok hp(44) == 22815088913;
+ok hp(45) == 3_411_949;
+
+# bigger cases;
+ok hp( 8) == 3331113965338635107;
+ok hp(20) == 3318308475676071413;
+
+# real 0m5.553s
+# user 0m5.535s
+# sys 0m0.016s
+