diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-02 16:50:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-02 16:50:07 +0000 |
| commit | 64d2dd490a4b7aed48c6ff98c017bd8ee25eb9a6 (patch) | |
| tree | dff9317532978c6b91261b2d897b6f2d135c9c1a | |
| parent | 2123c348502470f4ad8b4b70bdfbd4d202c9afa2 (diff) | |
| parent | 5722bc0b9c2594053d60dafd42c4f02d3ddb4314 (diff) | |
| download | perlweeklychallenge-club-64d2dd490a4b7aed48c6ff98c017bd8ee25eb9a6.tar.gz perlweeklychallenge-club-64d2dd490a4b7aed48c6ff98c017bd8ee25eb9a6.tar.bz2 perlweeklychallenge-club-64d2dd490a4b7aed48c6ff98c017bd8ee25eb9a6.zip | |
Merge pull request #5315 from pjcs00/branch-for-challenge-141
Committed solutions to challenge 141
| -rw-r--r-- | challenge-141/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-141/peter-campbell-smith/ch-1.pl | 56 | ||||
| -rwxr-xr-x | challenge-141/peter-campbell-smith/ch-2.pl | 56 |
3 files changed, 113 insertions, 0 deletions
diff --git a/challenge-141/peter-campbell-smith/blog.txt b/challenge-141/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..35ed0d154e --- /dev/null +++ b/challenge-141/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2021/12/perl-weekly-challenge-141-task-1-reads.html diff --git a/challenge-141/peter-campbell-smith/ch-1.pl b/challenge-141/peter-campbell-smith/ch-1.pl new file mode 100755 index 0000000000..24e5706fc1 --- /dev/null +++ b/challenge-141/peter-campbell-smith/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2021-11-29 +# PWC 141 task 1 + +use v5.20; +use warnings; +use strict; + +# Write a script to find lowest 10 positive integers having exactly 8 divisors. +# Example: 24 is the first such number having exactly 8 divisors: +# 1, 2, 3, 4, 6, 8, 12 and 24. + +# Some thoughts on this challenge: https://pjcs-pwc.blogspot.com/2021/12/perl-weekly-challenge-141-task-1-reads.html + +my ($test, @tests, $try, $divisor, $dividend, $key, $count, $string, $num, $how_many, $num_divisors, + $max_try, $z); + +# inputs - pairs of number of results to show and number of divisors required +@tests = ([10, 8]); +$max_try = 10000; + +# loop over tests +for $test (@tests) { + ($how_many, $num_divisors) = @$test; + say qq[\nSmallest $how_many numbers (up to $max_try) having $num_divisors divisors:]; + + # initialise + $num_divisors --; + $how_many -= 1; + $num = 0; + + # loop over integers + TRY: for $try (2..$max_try) { + $count = 0; # number of divisors found + $string = ''; # list of divisors + + # loop over potential divisors + for $divisor (1..int($try/2)) { + $dividend = $try/$divisor; + + # if it is a divisor + if ($dividend eq int($dividend)) { + $count ++; + $string .= qq[$divisor, ]; + next TRY if $count > $num_divisors; # give up if there are too many + } + } + + # if there were exactly the number of divisors requested we have an answer + if ($count == $num_divisors) { + say qq[$try has divisors $string$try]; # add the number itself to the list + last if $num ++ >= $how_many; # stop if we have enough answers + } + } +} diff --git a/challenge-141/peter-campbell-smith/ch-2.pl b/challenge-141/peter-campbell-smith/ch-2.pl new file mode 100755 index 0000000000..f9b706a078 --- /dev/null +++ b/challenge-141/peter-campbell-smith/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2021-12-01 +# PWC 141 task 2 + +use v5.20; +use warnings; +use strict; + +# You are given positive integers, $m and $n. +# Write a script to find total count of integers created using the digits +# of $m which is also divisible by $n. + +my (@tests, $test, $m, $n, $num_digits, @digits, $i, $iter, $c, + $extract, $all, $good, $num_good); + +use Algorithm::Combinatorics qw(combinations); + +# pairs of $m and $n +@tests = ([1234, 2], [768, 4], [1234567, 31]); + +# loop over pairs +for $test (@tests) { + $all = $good = ''; + $num_good = 0; + ($m, $n) = @$test; + + # split $m into an array of digits + $num_digits = length($m); + @digits = split(//, $m); + + # need all the combinations of 1, 2 ... (length - 1) digits + for $i (1..$num_digits - 1) { + $iter = combinations(\@digits, $i); + + # loop over combinations + while ($c = $iter->next) { + + # join digits of combination together + $extract = join('', @$c); + + # create string of $all combs and $good combs divisible by $n + $all .= qq[$extract, ]; + if ($extract % $n == 0) { + $good .= qq[$extract, ]; + $num_good ++; + } + } + } + + # trim trailing commas and show answer + $all =~ s|..$||; + $good =~ s|..$||; + say qq[Possible integers created using the digits of $m are:\n$all]; + say qq[There are $num_good integers divisible by $n which are:\n$good\n]; +} |
