diff options
| -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; +} |
