exercism / perl5

Exercism exercises in Perl 5.
https://exercism.org/tracks/perl5
MIT License
28 stars 51 forks source link

Generate full Perl tests for pythagorean-triplet #538

Closed m-dango closed 1 year ago

habere-et-dispertire commented 1 year ago

I cannot test due to a shell/BOM carton error so this is a blind solution for the pythagorean-triplet :

sub triplets_with_sum {
    my ($sum) = @_;
    my @solutions;
    for my $a ( 1 .. $sum ) {
        my $numerator   = 2*$a**2 + $sum**2 - 2*$sum*$a;
        my $denominator = 2*( $sum - $a );
        if ( $denominator > 0 and $numerator % $denominator == 0 ) {
            my $c = $numerator / $denominator;
            my $b = $sum - $a - $c;
            push @solutions, [ $a, $b, $c ] if $b > $a;
        }
    }
    return [ @solutions ];
}

--

$ carton install

XXX/.nix-profile/bin/carton: line 2: use: command not found
XXX/.nix-profile/bin/carton: line 3: use: command not found
XXX/.nix-profile/bin/carton: line 4: use: command not found
XXX/.nix-profile/bin/carton: line 6: syntax error near unexpected token `('
XXX/.nix-profile/bin/carton: line 6: `exit Carton::CLI->new->run(@ARGV);'