aboutsummaryrefslogtreecommitdiff
path: root/challenge-141
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-05 23:48:09 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-05 23:48:09 +0000
commitd80ab2433f6ad4ba5f1ecf8626ee18bb4f664d86 (patch)
treef57842fbd8c5f9e9b4f6d85880b6f8eac397bdad /challenge-141
parent3537afcbeb171df4cd95e9c649c6b5093fef0493 (diff)
downloadperlweeklychallenge-club-d80ab2433f6ad4ba5f1ecf8626ee18bb4f664d86.tar.gz
perlweeklychallenge-club-d80ab2433f6ad4ba5f1ecf8626ee18bb4f664d86.tar.bz2
perlweeklychallenge-club-d80ab2433f6ad4ba5f1ecf8626ee18bb4f664d86.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-141')
-rwxr-xr-xchallenge-141/colin-crain/perl/ch-1.pl80
-rwxr-xr-xchallenge-141/colin-crain/perl/ch-2.pl85
2 files changed, 165 insertions, 0 deletions
diff --git a/challenge-141/colin-crain/perl/ch-1.pl b/challenge-141/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..c02a84d5a3
--- /dev/null
+++ b/challenge-141/colin-crain/perl/ch-1.pl
@@ -0,0 +1,80 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# octosplitter.pl
+#
+# Number Divisors
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# method:
+# determining a progressive group of factor sets that will
+# produce the lowest integers with only 8 factors is...
+# complex. We'll settle for complex.
+#
+# It's hardly obvious, and it seems that the first thing to do,
+# should we want to study the sets and see if we can draw some
+# inferances, is to find the solutions and have a look.
+#
+# This of course brings us full circle and solves our problem
+# by... solving our problem. Of course. Why didn't I think of
+# that before?
+#
+# In any case we can always try every number from 2 up to half
+# the target, and add on 1 and the target, as apparently we're
+# including those this time. Fair enough. By extension we also
+# include the number itself. Note we aren't being asked for
+# *prime* factors, so for example should the case arrive both 2
+# and 4 should be counted, so we need to try every value in the
+# range.
+#
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+
+
+sub nd_brute ( $num, @div ) {
+ $num % $_ or push @div, $_ for 2..$num/2 ;
+ return 1, @div, $num;
+}
+
+say "number divisors";
+say "----------------------------------------";
+my ($test, $count) = (0,0);
+while ( $count < 9 ) {
+ my @facts = nd_brute( ++$test );
+ if (@facts == 8) {
+ say "$test ", sprintf "%4d" x 8 , @facts ;
+ $count++;
+ };
+}
+
+
+
+
+
+
+
+
+# use Test::More;
+#
+# is
+#
+# done_testing();
diff --git a/challenge-141/colin-crain/perl/ch-2.pl b/challenge-141/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..88ed16d58b
--- /dev/null
+++ b/challenge-141/colin-crain/perl/ch-2.pl
@@ -0,0 +1,85 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# i-lk-u.pl
+#
+# Like Numbers
+# Submitted by: Mohammad S Anwar
+# 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.
+#
+# Repeating of digits are not allowed. Order/Sequence of digits
+# can’t be altered. You are only allowed to use (n-1) digits at the
+# most. For example, 432 is not acceptable integer created using
+# the digits of 1234. Also for 1234, you can only have integers
+# having no more than three digits.
+#
+# Example 1:
+#
+# Input: $m = 1234, $n = 2
+# Output: 9
+#
+# Possible integers created using the digits of 1234 are:
+# 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234.
+#
+# There are 9 integers divisible by 2 such as:
+# 2, 4, 12, 14, 24, 34, 124, 134 and 234.
+#
+# Example 2:
+#
+# Input: $m = 768, $n = 4
+# Output: 3
+#
+# Possible integers created using the digits of 768 are:
+# 7, 6, 8, 76, 78 and 68.
+#
+# There are 3 integers divisible by 4 such as:
+# 8, 76 and 68.
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+my $m = shift @ARGV // 76876522;
+my $n = shift @ARGV // 143;
+
+say "input number m : ", $m;
+say "input divisor n : ", $n;
+say "integers found : ", join ', ', get_divs( $n, get_ints( $m ));
+
+sub get_ints( $num ) {
+ my $len = length($num);
+ my @bins = map { sprintf "%0${len}b", $_ } (1 .. 2**$len - 1);
+ my @out;
+
+ for my $b ( @bins ) {
+ my $combi;
+ for my $idx (0..$len-1) {
+ $combi .= (substr $b, $idx, 1)
+ ? substr $num, $idx, 1
+ : ''
+ }
+ push @out, $combi unless $combi == $num;
+ }
+
+ return sort {$a<=>$b} @out;
+}
+
+
+sub get_divs ( $div, @nums ) {
+ return grep { not $_ % $div } @nums;
+
+}
+
+
+