aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-12 14:14:20 +0000
committerGitHub <noreply@github.com>2022-01-12 14:14:20 +0000
commit9bbcf9872d1b574ba0bb28a38d53b7f5a58718e4 (patch)
treee3af6d5f525bade727db691e3470793f57997aa2
parent4ae320d10003e1038f530214a79c9ef89765130c (diff)
parent9b1f239ffecd7923ede7ba8d965939fd08fb5661 (diff)
downloadperlweeklychallenge-club-9bbcf9872d1b574ba0bb28a38d53b7f5a58718e4.tar.gz
perlweeklychallenge-club-9bbcf9872d1b574ba0bb28a38d53b7f5a58718e4.tar.bz2
perlweeklychallenge-club-9bbcf9872d1b574ba0bb28a38d53b7f5a58718e4.zip
Merge pull request #5509 from jacoby/master
Blogged, Plus Minor Changes.
-rw-r--r--challenge-147/dave-jacoby/blog.txt1
-rw-r--r--challenge-147/dave-jacoby/perl/ch-1.pl35
-rw-r--r--challenge-147/dave-jacoby/perl/ch-2.pl35
3 files changed, 71 insertions, 0 deletions
diff --git a/challenge-147/dave-jacoby/blog.txt b/challenge-147/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..e386698c3c
--- /dev/null
+++ b/challenge-147/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2022/01/11/truncations-and-pentagons-the-weekly-challenge-147.html \ No newline at end of file
diff --git a/challenge-147/dave-jacoby/perl/ch-1.pl b/challenge-147/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..197c72f5b7
--- /dev/null
+++ b/challenge-147/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+my %primes;
+my %trunc;
+my $c = 1;
+my $n = 2;
+
+while (1) {
+ if ( $n !~ /0/mx && is_prime($n) ) {
+ $primes{$n}++;
+ my $copy = $n;
+ while ( length $copy > 0 ) {
+ last unless $primes{$copy};
+ substr( $copy, 0, 1 ) = '';
+ if ( $copy eq '' ) {
+ $trunc{$n}++ if $copy eq '';
+ last;
+ }
+ }
+ last if scalar keys %trunc > 30;
+ }
+ $n++;
+}
+
+say join ', ', sort { $a <=> $b } keys %trunc;
+
+sub is_prime ($n) {
+ for ( 2 .. sqrt $n ) { return unless $n % $_ }
+ return 1;
+}
diff --git a/challenge-147/dave-jacoby/perl/ch-2.pl b/challenge-147/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..61f3b1b1d6
--- /dev/null
+++ b/challenge-147/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+my $top = 10_000;
+my @pentagon = map { pentagon($_) } 0 .. $top;
+my %pentagon = map { $_ => 1 } @pentagon;
+delete $pentagon{0};
+
+for my $i ( 1 .. $top ) {
+ for my $j ( 1 .. $i ) {
+ my $pi = $pentagon[$i];
+ my $pj = $pentagon[$j];
+ my $sum = $pi + $pj;
+ if ( $pentagon{$sum} ) {
+ my $product = abs( $pi - $pj );
+ if ( $pentagon{$product} ) {
+ say <<"END";
+ P($i) = $pi
+ P($j) = $pj
+ $pi + $pj = $sum
+ abs( $pi - $pj ) = $product
+END
+ exit;
+ }
+ }
+ }
+}
+
+sub pentagon ( $n ) {
+ return $n * ( ( $n * 3 ) - 1 ) / 2;
+}