aboutsummaryrefslogtreecommitdiff
path: root/challenge-089/miguel-prz/perl/Task089_1.pm
blob: 77fe68a858bb1a33a0e06e996cd07a7ec5a0ebb8 (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
use strict;
use warnings;

# calc GCD Euclid's algorithm
# https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm

sub gcd {
    my ($x, $y) = @_;

    $y ? gcd($y, $x % $y) : $x;
}

# sum GCD of all possible unique pairs between 1 and $N

sub sum_gcd_unique_pairs {
    my $N = shift;
    my $sum = 0;

    for my $i (1 .. $N) {
        for my $j ($i+1 .. $N) {
            $sum += gcd($i, $j);
        }
    }
    return $sum;
}


1970;