aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-143/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-143/peter-campbell-smith/perl/ch-1.pl21
-rwxr-xr-xchallenge-143/peter-campbell-smith/perl/ch-2.pl38
3 files changed, 60 insertions, 0 deletions
diff --git a/challenge-143/peter-campbell-smith/blog.txt b/challenge-143/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..a1e7aec8b1
--- /dev/null
+++ b/challenge-143/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://www.blogger.com/blog/posts/6451878819927982087
diff --git a/challenge-143/peter-campbell-smith/perl/ch-1.pl b/challenge-143/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..33ca2989b7
--- /dev/null
+++ b/challenge-143/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-12-13
+# PWC 143 task 1
+
+use v5.20;
+use warnings;
+use strict;
+
+# You are given a string, $s, containing mathematical expression.
+# Write a script to print the result of the mathematical expression.
+
+my ($test, @tests);
+
+# inputs
+@tests = ("10 + 20 - 5", "(10 + 20 - 5) * 2", "(12345 * 6789) / 23 + 1 / 77");
+
+# eval will do it
+for $test (@tests) {
+ say qq[\nInput: \$s = $test\nOutput: ] . eval($test);
+}
diff --git a/challenge-143/peter-campbell-smith/perl/ch-2.pl b/challenge-143/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..9dee2b5f70
--- /dev/null
+++ b/challenge-143/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-12-13
+# PWC 143 task 2
+
+use v5.20;
+use warnings;
+use strict;
+
+use Math::Prime::Util qw(divisors);
+use Algorithm::Combinatorics qw(variations);
+
+# You are given a positive number, $n.
+# Write a script to find out if the given number is Stealthy Number.
+# A positive integer N is stealthy, if there exist positive integers a, b, c, d
+# such that a * b = c * d = N and a + b = c + d + 1.
+
+my ($test, @tests, @divisors, $variations, $v, $good, $half);
+
+# inputs
+@tests = (36, 12, 6, 22, 23, 24, 8424, 7200, 4);
+
+# eval will do it
+for $test (@tests) {
+ $good = 0;
+ @divisors = divisors($test);
+ $variations = variations(\@divisors, 2);
+ $half = sqrt($test);
+ while ($v = $variations->next) {
+ next unless ($v->[0] <= $half and $v->[1] <= $half);
+ if ($v->[0] + $test / $v->[0] == $v->[1] + $test / $v->[1] + 1) {
+ say qq[\nInput: $test\nOutput: 1] if $good == 0;
+ say qq[$v->[0] + ] . ($test / $v->[0]) . qq[ == $v->[1] + ] . ($test / $v->[1]) . qq[ + 1];
+ $good ++;
+ }
+ }
+ say qq[\nInput: $test\nOutput: 0] unless $good;
+}