diff options
| author | Alexander Karelas <karjala@cpan.org> | 2022-01-12 00:01:23 +0200 |
|---|---|---|
| committer | Alexander Karelas <karjala@cpan.org> | 2022-01-12 00:01:23 +0200 |
| commit | 9268e1cb3a4fcc65fe025e8e6d2780136294c180 (patch) | |
| tree | 0f6181dda49b5103fe324031293dea68d29a6c07 | |
| parent | 08a76ad16ee62b2cbb2cda3508445047f2ff9cf1 (diff) | |
| download | perlweeklychallenge-club-9268e1cb3a4fcc65fe025e8e6d2780136294c180.tar.gz perlweeklychallenge-club-9268e1cb3a4fcc65fe025e8e6d2780136294c180.tar.bz2 perlweeklychallenge-club-9268e1cb3a4fcc65fe025e8e6d2780136294c180.zip | |
my solution to 147-2
| -rwxr-xr-x | challenge-147/alexander-karelas/perl/ch2.pl | 30 |
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; + } + } +} |
