diff options
| -rw-r--r-- | challenge-133/abigail/README.md | 6 | ||||
| -rw-r--r-- | challenge-133/abigail/perl/ch-1a.pl | 36 |
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 <>; |
