diff options
Diffstat (limited to 'challenge-125')
| -rw-r--r-- | challenge-125/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-125/laurent-rosenfeld/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-125/laurent-rosenfeld/raku/ch-1.raku | 20 |
3 files changed, 47 insertions, 0 deletions
diff --git a/challenge-125/laurent-rosenfeld/blog.txt b/challenge-125/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..9988c34d38 --- /dev/null +++ b/challenge-125/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2021/08/perl-weekly-challenge-125-pythagorean-triples.html diff --git a/challenge-125/laurent-rosenfeld/perl/ch-1.pl b/challenge-125/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..140b06e20c --- /dev/null +++ b/challenge-125/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +use feature qw/say/; + +my $max = 300; +my @squares = map $_ * $_ , 1..$max; +my %square_hash = map { $_ => 1 } @squares; +my @combinations; +for my $i (2..200) { + push @combinations, [$i, $_] for $i+1 .. $max; +} +my @triples; +for my $comb (@combinations) { + my $sum_sq = $comb->[0] ** 2 + $comb->[1] ** 2; + push @triples, [ @$comb, 0 + $sum_sq ** 0.5 ] if exists $square_hash{$sum_sq}; +} +my %look_up = (0 => " [ -1 ] ", 1 => " [ -1 ] ", 2 => " [ -1 ] " ); +for my $triple (@triples) { + for my $val (@$triple) { + $look_up{$val} .= " [ @$triple ] " ; + } +} +for my $test (1..30) { + my $result = $look_up{$test}; + say "$test:\t $result"; +} diff --git a/challenge-125/laurent-rosenfeld/raku/ch-1.raku b/challenge-125/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..22987818cb --- /dev/null +++ b/challenge-125/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,20 @@ +use v6; + +my @squares = map { $_² }, 1..Inf; +my $max = 200; +my $square-set = @squares[0..$max]; +my @square-triples = gather { + for (@squares[0..$max]).combinations(2) -> $comb { + my $sum = [+] $comb; + take (|$comb, $sum) if $sum (elem) $square-set; + } +} +# say @square-triples; +my %look-up = 0 => -1, 1 => -1, 2 => -1; +for @square-triples -> $triple { + push %look-up, $triple[$_].sqrt => (map { $_.sqrt}, $triple[0..2]) for 0..2; +} +# say %look-up{13}; +for 1..20 -> $test { + say "$test:\t", %look-up{$test}; +} |
