aboutsummaryrefslogtreecommitdiff
path: root/challenge-147
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-12 09:30:30 +0000
committerGitHub <noreply@github.com>2022-01-12 09:30:30 +0000
commitd5c9827b7280af3de2df9a894e2a4228f9e5d272 (patch)
tree91f29e869db8ca83207b70101bbfac78622528d3 /challenge-147
parenta78d6b4ef967f008fbbaf21eae41d3014cdb4b10 (diff)
parent9268e1cb3a4fcc65fe025e8e6d2780136294c180 (diff)
downloadperlweeklychallenge-club-d5c9827b7280af3de2df9a894e2a4228f9e5d272.tar.gz
perlweeklychallenge-club-d5c9827b7280af3de2df9a894e2a4228f9e5d272.tar.bz2
perlweeklychallenge-club-d5c9827b7280af3de2df9a894e2a4228f9e5d272.zip
Merge pull request #5506 from akarelas/pr-akarelas
my solution to 147-2
Diffstat (limited to 'challenge-147')
-rwxr-xr-xchallenge-147/alexander-karelas/perl/ch2.pl30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-147/alexander-karelas/perl/ch2.pl b/challenge-147/alexander-karelas/perl/ch2.pl
new file mode 100755
index 0000000000..b6548da74a
--- /dev/null
+++ b/challenge-147/alexander-karelas/perl/ch2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use v5.32;
+use warnings;
+
+use experimental 'signatures';
+
+my @cache;
+my %cache;
+sub get_nth_pentagon_number ($n) {
+ my $pentagon = $cache[$n];
+ $pentagon //= do {
+ $cache[$n] = $n * (3 * $n - 1) / 2;
+ $cache{ $cache[$n] } = $n;
+ }
+}
+
+for (my $i = 1; ; $i++) {
+ my $ith = get_nth_pentagon_number($i);
+ get_nth_pentagon_number(2 * $i - 1);
+ get_nth_pentagon_number(2 * $i);
+ for (my $j = 1; $j < $i; $j++) {
+ my $jth = get_nth_pentagon_number($j);
+ if (exists $cache{$ith + $jth} and exists $cache{$ith - $jth}) {
+ say "P($i) + P($j) = $ith + $jth = @{[ $ith + $jth ]} = P(", $cache{$ith + $jth}, ")";
+ say "P($i) - P($j) = $ith - $jth = @{[ $ith - $jth ]} = P(", $cache{$ith - $jth}, ")";
+ exit;
+ }
+ }
+}