aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-10-26 18:42:18 +0100
committerGitHub <noreply@github.com>2019-10-26 18:42:18 +0100
commitde7574f04f7a09539ee1e146417bf688b16a3cde (patch)
treed5bd6ddec3e2ca02b455d9085c08ef8a9700bc10
parentc8eaa289c650e6d69a4efbfe4426a4e2279525e4 (diff)
parent3b50cfa06f83676fcac4fa665c06951be6e8446e (diff)
downloadperlweeklychallenge-club-de7574f04f7a09539ee1e146417bf688b16a3cde.tar.gz
perlweeklychallenge-club-de7574f04f7a09539ee1e146417bf688b16a3cde.tar.bz2
perlweeklychallenge-club-de7574f04f7a09539ee1e146417bf688b16a3cde.zip
Merge pull request #839 from noudald/challenge-031-noud
Solutions to challenge 031 problem 1 and 2 in Perl 6
-rw-r--r--challenge-031/noud/perl6/ch1.p639
-rw-r--r--challenge-031/noud/perl6/ch2.p614
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-031/noud/perl6/ch1.p6 b/challenge-031/noud/perl6/ch1.p6
new file mode 100644
index 0000000000..bfd302eeac
--- /dev/null
+++ b/challenge-031/noud/perl6/ch1.p6
@@ -0,0 +1,39 @@
+# Create a function to check divide by zero error without checking if the
+# denominator is zero.
+
+# Normally you would try to divide by zero, catch the exception that's
+# generated while dividing by zero.
+#
+# For fun, I'll try something else. Suppose we want to compute z = x / y. To
+# compute z we project x and y onto a one dimensional complex sphere with
+# center i / 2 and radius 1/2, and compute the division on the complex sphere.
+# This project is called the stereographic projects, also see
+# https://en.wikipedia.org/wiki/Stereographic_projection. The stereographic
+# projects of x is
+#
+# x |---> \hat(x) = x / (1 + x^2) + x^2 / (1 + x^2) * i
+#
+# In the stereographic projection we can divide by zero. After dividing by
+# zero we end up in the north pole, which is the complex number i. Hence,
+# instead of checking if the denominator is zero, we check if in the
+# stereographic projection \hat(x / y) = i. Therefore, we don't check the if
+# the denominator is zero explicitly. A straightforward computation gives
+#
+# \hat(x / y) = xy / (x^2 + y^2) + x^2 / (x^2 + y^2) * i
+
+sub infix:<%/>($x, $y) {
+ my $z = Complex.new($x * $y / ($x**2 + $y**2), $x**2 / ($x**2 + $y**2));
+ if ($z === i) {
+ # If z is the north pole, the inverse stereographic projection is
+ # not a number. (this is actually the perl weekly challenge)
+ NaN;
+ } else {
+ # For fun, use the inverse stereographic projection to compute x / y.
+ $z.re / (1 - $z.im);
+ }
+}
+
+say 1 %/ 2;
+say 3 %/ 7;
+say 1 %/ 0;
+say 0 %/ 0;
diff --git a/challenge-031/noud/perl6/ch2.p6 b/challenge-031/noud/perl6/ch2.p6
new file mode 100644
index 0000000000..a2afe90d4e
--- /dev/null
+++ b/challenge-031/noud/perl6/ch2.p6
@@ -0,0 +1,14 @@
+# Create a script to demonstrate creating dynamic variable name, assign a value
+# to the variable and finally print the variable. The variable name would be
+# passed as command line argument.
+
+# Example usage:
+#
+# > perl6 ch2.p6 my_var 37
+# $my_var = 37
+#
+
+sub MAIN($name, $value) {
+ GLOBAL::{'$' ~ $name} = $value;
+ say '$' ~ $name ~ " = " ~ GLOBAL::{'$' ~ $name};
+}