aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-30 09:40:57 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-30 09:40:57 +0000
commit435ad4a3ab32f0d490b91c921a1164a6b11f1f29 (patch)
tree1131538d7833a0760f20d65099386ca8f59b7329
parenta53828c517ea7368d0efc3f64779bd0853c17330 (diff)
downloadperlweeklychallenge-club-435ad4a3ab32f0d490b91c921a1164a6b11f1f29.tar.gz
perlweeklychallenge-club-435ad4a3ab32f0d490b91c921a1164a6b11f1f29.tar.bz2
perlweeklychallenge-club-435ad4a3ab32f0d490b91c921a1164a6b11f1f29.zip
Add Perl solution to challenge 141
-rw-r--r--challenge-141/paulo-custodio/perl/ch-1.pl44
-rw-r--r--challenge-141/paulo-custodio/perl/ch-2.pl68
-rw-r--r--challenge-141/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-141/paulo-custodio/t/test-2.yaml10
4 files changed, 137 insertions, 0 deletions
diff --git a/challenge-141/paulo-custodio/perl/ch-1.pl b/challenge-141/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..e012a7e143
--- /dev/null
+++ b/challenge-141/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+# Challenge 141
+#
+# TASK #1 > 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.
+
+use Modern::Perl;
+
+use constant NUM_DIVISORS => 8;
+
+sub divisors {
+ my($n) = @_;
+ my(@div_low, @div_high);
+ for (my $i = 1; $i <= sqrt($n); $i++) {
+ if ($n%$i == 0) {
+ push @div_low, $i;
+ unshift @div_high, $n/$i if $n/$i != $i;
+ }
+ }
+ return (@div_low, @div_high);
+}
+
+sub next_number {
+ my($n) = @_;
+
+ for (;;) {
+ $n++;
+ my @divisors = divisors($n);
+ return $n if @divisors == NUM_DIVISORS;
+ }
+}
+
+my $num = shift||10;
+my $n = 0;
+for (1..$num) {
+ $n = next_number($n);
+ say $n;
+}
diff --git a/challenge-141/paulo-custodio/perl/ch-2.pl b/challenge-141/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..fb86077c0c
--- /dev/null
+++ b/challenge-141/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+# Challenge 141
+#
+# TASK #2 > 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.
+
+use Modern::Perl;
+
+sub numbers {
+ my($num) = @_;
+ my @ret;
+
+ my $mask_n = 0;
+ for (;; $mask_n++) {
+ my $mask = sprintf("%0".length($num)."b", $mask_n);
+ last if length($mask) > length($num);
+
+ # combine $num with $mask
+ my $res = 0;
+ for my $i (0 .. length($num)-1) {
+ if (substr($mask, $i, 1) eq "1") {
+ $res = 10*$res + substr($num, $i, 1);
+ }
+ }
+ push @ret, $res;
+ }
+ return @ret;
+}
+
+my($m, $n) = @ARGV;
+my $count = 0;
+for my $num (numbers($m)) {
+ if ($num != 0 && $num != $m) {
+ if ($num % $n == 0) {
+ $count++;
+ }
+ }
+}
+say $count;
diff --git a/challenge-141/paulo-custodio/t/test-1.yaml b/challenge-141/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1a802e4e2c
--- /dev/null
+++ b/challenge-141/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: |
+ 24
+ 30
+ 40
+ 42
+ 54
+ 56
+ 66
+ 70
+ 78
+ 88
diff --git a/challenge-141/paulo-custodio/t/test-2.yaml b/challenge-141/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..ce5c683394
--- /dev/null
+++ b/challenge-141/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1234 2
+ input:
+ output: 9
+- setup:
+ cleanup:
+ args: 768 4
+ input:
+ output: 3