diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-10-19 06:09:25 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-10-19 06:09:25 +0100 |
| commit | d7cc7d8fd62bb12e0129ac28c7c0bc211f560397 (patch) | |
| tree | f79adaa02dd53ca2507bac10ac52d82874b2bbc5 /challenge-083/andinus | |
| parent | 1435dcfd265afbba3b165aa738f720b250714187 (diff) | |
| download | perlweeklychallenge-club-d7cc7d8fd62bb12e0129ac28c7c0bc211f560397.tar.gz perlweeklychallenge-club-d7cc7d8fd62bb12e0129ac28c7c0bc211f560397.tar.bz2 perlweeklychallenge-club-d7cc7d8fd62bb12e0129ac28c7c0bc211f560397.zip | |
- Added template for Challenge 83.
Diffstat (limited to 'challenge-083/andinus')
| -rw-r--r-- | challenge-083/andinus/README | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/challenge-083/andinus/README b/challenge-083/andinus/README new file mode 100644 index 0000000000..2ad8a0f3af --- /dev/null +++ b/challenge-083/andinus/README @@ -0,0 +1,60 @@ + ━━━━━━━━━━━━━━━ + CHALLENGE 082 + + Andinus + ━━━━━━━━━━━━━━━ + + +Table of Contents +───────────────── + +1. Task 1 - Common Factors +.. 1. Perl + + + + + +1 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. + + +1.1 Perl +──────── + + • Program: <file:perl/ch-1.pl> + + 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"; + └──── |
