aboutsummaryrefslogtreecommitdiff
path: root/challenge-246/dave-jacoby/perl/ch-2.pl
blob: 9ff50ee9475b898582eaceae39dfccfa7d8cbf94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ say postderef signatures state };

my @examples = (

    [ 1, 1, 2, 3,  5 ],
    [ 4, 2, 4, 5,  7 ],
    [ 4, 1, 2, -3, 8 ],
);

for my $e (@examples) {
    my $input  = join ', ', $e->@*;
    my $output = lrso( $e->@* );

    say <<~"END";
    Input:  \$input = ($input)
    Output:          $output
    END
}

sub lrso (@input) {
OUTER: for my $n ( 2 .. -1 + scalar @input ) {
        for my $p ( -100 .. 100 ) {
            my $pp = $p * $input[ $n - 2 ];
            for my $q ( -100 .. 100 ) {
                my $qq = $q * $input[ $n - 1 ];
                my $rr = $pp + $qq;
                next OUTER if $rr == $input[$n];
            }
        }
        return 'false';
    }
    return 'true';
}