aboutsummaryrefslogtreecommitdiff
path: root/challenge-147
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-13 16:20:42 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-13 16:20:42 +0000
commitd17a75ecd7bd46d07c62f088394f854c355f917b (patch)
tree319ffb201fdf07e98b7e58d254a8497751be4edb /challenge-147
parentc29113a8df14bf180f5d3cd4dd7d9b0d9bd5fc46 (diff)
downloadperlweeklychallenge-club-d17a75ecd7bd46d07c62f088394f854c355f917b.tar.gz
perlweeklychallenge-club-d17a75ecd7bd46d07c62f088394f854c355f917b.tar.bz2
perlweeklychallenge-club-d17a75ecd7bd46d07c62f088394f854c355f917b.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-147')
-rw-r--r--challenge-147/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-147/laurent-rosenfeld/perl/ch-1.pl35
-rw-r--r--challenge-147/laurent-rosenfeld/perl/ch-2.pl20
-rw-r--r--challenge-147/laurent-rosenfeld/raku/ch-1.raku17
-rw-r--r--challenge-147/laurent-rosenfeld/raku/ch-2.raku12
5 files changed, 85 insertions, 0 deletions
diff --git a/challenge-147/laurent-rosenfeld/blog.txt b/challenge-147/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..bf530ed6a2
--- /dev/null
+++ b/challenge-147/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2022/01/perl-weekly-challenge-147-truncatable-primes-and-pentagon-numbers.html
diff --git a/challenge-147/laurent-rosenfeld/perl/ch-1.pl b/challenge-147/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..43baf13ae9
--- /dev/null
+++ b/challenge-147/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,35 @@
+use strict;
+use warnings;
+use feature "say";
+use constant MAX => 20;
+
+my @primes = (2, 3, 5);
+my %primes_h = map {$_ => 1} @primes;
+my @truncatables = @primes;;
+my $candidate = $primes[-1];
+my $count = scalar @truncatables;;
+while ($count < MAX) {
+ $candidate += 2;
+ my $not_prime = 0;
+ next if $candidate =~ /0/;
+ my $sq_cand = sqrt $candidate;
+ for my $i (@primes) {
+ $not_prime = 1, last unless $candidate % $i;
+ last if $i > $sq_cand;
+ }
+ next if $not_prime;
+ push @primes, $candidate;
+ $primes_h{$candidate} = 1;
+ # now check if truncatable prime
+ my $length = length $candidate;
+ my $is_truncatable = 1;
+ for my $i (1..$length) {
+ my $truncated = substr $candidate, $length - $i;
+ $is_truncatable = 0, last unless exists $primes_h{$truncated};
+ }
+ if ($is_truncatable) {
+ push @truncatables, $candidate;
+ $count++;
+ }
+}
+say "@truncatables";
diff --git a/challenge-147/laurent-rosenfeld/perl/ch-2.pl b/challenge-147/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..ea57d5b72c
--- /dev/null
+++ b/challenge-147/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use feature "say";
+use constant MAX => 4000;
+
+my @pentanums = map { $_ * (3 * $_ - 1)/2 } 1..MAX;
+my %penta_h = map {$pentanums[$_] => $_+1 } 0..MAX-1;
+# say Dumper \%penta_h;
+
+OUTER: for my $i (0..MAX-1) {
+ for my $j ($i+1..MAX-1) {
+ my $sum = $pentanums[$i] + $pentanums[$j];
+ next unless exists $penta_h{$sum};
+ my $diff = $pentanums[$j] - $pentanums[$i];
+ next unless exists $penta_h{$diff};
+ say "First pair of pentagon numbers is $pentanums[$i] (rank ", $i+1, ") and $pentanums[$j] (rank ", $j+1, ")";
+ say "Sum is $sum (rank $penta_h{$sum}) and difference is $diff (rank $penta_h{$diff})";
+ last OUTER;
+ }
+}
diff --git a/challenge-147/laurent-rosenfeld/raku/ch-1.raku b/challenge-147/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..e8ed2b5205
--- /dev/null
+++ b/challenge-147/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,17 @@
+my @truncatables;
+my $count = 0;
+my @el-primes = grep {.is-prime and not /0/}, 2..Inf;
+for @el-primes -> $candidate {
+ my $length = $candidate.chars;
+ my $is-truncatable = True;
+ for 1..$length -> $i {
+ my $truncated = substr $candidate, $length - $i;
+ $is-truncatable = False, last unless $truncated.is-prime;
+ }
+ if $is-truncatable {
+ push @truncatables, $candidate;
+ $count++;
+ }
+ last if $count >= 20;
+}
+say @truncatables;
diff --git a/challenge-147/laurent-rosenfeld/raku/ch-2.raku b/challenge-147/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..3099a8d9ea
--- /dev/null
+++ b/challenge-147/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,12 @@
+my $max = 4000;
+my @pentanums = map { (3 * $_² - $_)/2 }, 1..$max;
+my %penta = map {@pentanums[$_] => $_+1}, 0..$max-1;
+for @pentanums.combinations(2) -> $comb {
+ next unless %penta{$comb.sum}:exists;
+ next unless %penta{$comb[1]-$comb[0]}:exists;
+ say $comb, " = Pentagon numbers N° %penta{$comb[0]} and %penta{$comb[1]}";
+ say "Sum is ", $comb.sum, " (Pentagon number ", %penta{$comb.sum}, ")";
+ say "Difference is ", $comb[1]-$comb[0], " (Pentagon number ", %penta{$comb[1]-$comb[0]}, ")";
+ last;
+}
+say now - INIT now, " seconds";