UP / HOME

Challenge 082

Table of Contents

Task 1 - Common Factors

You are given 2 positive numbers $M and $N.

Write a script to list all common factors of the given numbers.

Perl

We loop over all the numbers from 1 ... $M to get their factors & then just compare it with factors of $N. I took this code from Challenge 081's ch-1.pl.

my $A = shift @ARGV;
my $B = shift @ARGV;

# Get all common divisors.
my %divisors_of_A = divisors($A);
my %divisors_of_B = divisors($B);
my @common_divisors;
foreach my $num (sort { $a <=> $b } keys %divisors_of_A) {
    push @common_divisors, $num
        if exists $divisors_of_B{$num};
}

# Returns all divisors of a number.
sub divisors {
    my $n = shift @_;
    my %divisors;
    foreach my $i ( 1 ... $n){
        if ($n % $i == 0) {
            $divisors{$i} = 1;
        }
    }
    return %divisors;
}

print join(', ', @common_divisors), "\n";

Andinus / / Modified: 2020-10-20 Tue 18:22 Emacs 27.1 (Org mode 9.4)