diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2022-01-10 19:44:04 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2022-01-10 19:44:04 -0500 |
| commit | f5f9c8a7c5b5e6e640c6e52579878e0ed5cc97fa (patch) | |
| tree | 52db3cd49f4c4fb5f53fcd12a578c72050eeab61 | |
| parent | e9411bdc7658179af3f23d3ada7970323547a7d7 (diff) | |
| download | perlweeklychallenge-club-f5f9c8a7c5b5e6e640c6e52579878e0ed5cc97fa.tar.gz perlweeklychallenge-club-f5f9c8a7c5b5e6e640c6e52579878e0ed5cc97fa.tar.bz2 perlweeklychallenge-club-f5f9c8a7c5b5e6e640c6e52579878e0ed5cc97fa.zip | |
147 - 3 * 7 * 7
| -rw-r--r-- | challenge-147/dave-jacoby/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-147/dave-jacoby/perl/ch-2.pl | 37 |
2 files changed, 72 insertions, 0 deletions
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..edc998ee77 --- /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 ( 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 > 20; + } + $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..4385f57450 --- /dev/null +++ b/challenge-147/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +my $top = 100_000; +my @pentagon = map { pentagon($_) } 0 .. $top; +my %pentagon = map { $_ => 1 } @pentagon; + +for my $i ( 1 .. $top ) { + for my $j ( 1 .. $i - 1 ) { + 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; +} |
