aboutsummaryrefslogtreecommitdiff
path: root/challenge-252/dave-jacoby/perl/ch-1.pl
blob: f1b874189f8d64f25098ea6d583585127b85d864 (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
38
39
40
41
42
#!/usr/bin/env perl

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

use List::Util qw{ sum0 };

my @examples = (

    [ 1, 2, 3, 4 ],
    [ 2, 7, 1, 19, 18, 3 ],
);

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

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

sub special_numbers (@input) {
    my $output = 0;
    my $n      = scalar @input;
    return 
        sum0 
        map { $input[ $_ - 1 ] ** 2 } 
        grep { $n % $_ == 0 } 
        1 .. scalar @input;

    ## the longer form I wrote first
    # for my $i ( 1 .. scalar @input ) {
    #     if ( $n % $i == 0 ) {
    #         my $v = $input[ $i - 1 ];
    #         $output += ( $v**2 );
    #     }
    # }
    # return $output;
}