aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-11-01 17:34:39 +0100
committerAbigail <abigail@abigail.be>2021-11-01 17:34:39 +0100
commit4f367cc84f76448eb21a5bda0bb4771f1118d09f (patch)
tree1ad482838d413ea0cd9b592f1ed6adac9b88f99b
parentc97941ff8e30ea3fdb098a9cfdd6050a002478b6 (diff)
downloadperlweeklychallenge-club-4f367cc84f76448eb21a5bda0bb4771f1118d09f.tar.gz
perlweeklychallenge-club-4f367cc84f76448eb21a5bda0bb4771f1118d09f.tar.bz2
perlweeklychallenge-club-4f367cc84f76448eb21a5bda0bb4771f1118d09f.zip
Alternative Perl solution for week 133, part 1
-rw-r--r--challenge-133/abigail/README.md6
-rw-r--r--challenge-133/abigail/perl/ch-1a.pl36
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-133/abigail/README.md b/challenge-133/abigail/README.md
index d51d3d73e2..bbf706a1f5 100644
--- a/challenge-133/abigail/README.md
+++ b/challenge-133/abigail/README.md
@@ -2,8 +2,14 @@
## Part 1
+### "No buildin function at all"
+
* [Perl](perl/ch-1.pl)
+### "No buildin sqrt function"
+
+* [Perl][perl/ch-1a.pl)
+
## Part 2
* [C](c/ch-2.c)
diff --git a/challenge-133/abigail/perl/ch-1a.pl b/challenge-133/abigail/perl/ch-1a.pl
new file mode 100644
index 0000000000..dfe877ae0f
--- /dev/null
+++ b/challenge-133/abigail/perl/ch-1a.pl
@@ -0,0 +1,36 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-1a.pl < input-file
+#
+
+#
+# Unlike ch-1.pl, this is a solution which only avoids sqrt (and int for
+# that matter)
+#
+# Given that:
+#
+# * sqrt (a) == a^(1/2)
+# * a^b == exp (log (a^b)), a > 0
+# * log (a^b) == b * log (a), a > 0
+#
+# We can write sqrt (N) as exp (log (N) / 2)
+#
+# We avoid using int() by just stripping the decimal point and
+# anything following it using a regular expression.
+#
+
+say (exp (log ($_) / 2) =~ s/\..*//r) while <>;