diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-10-26 19:28:40 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2019-10-26 19:28:40 +0200 |
| commit | 3b50cfa06f83676fcac4fa665c06951be6e8446e (patch) | |
| tree | 9ce0a20fcd769e25c30cc98a4636ca7c748f41a0 | |
| parent | eebcb87b5b57eef54424e41660dc5881e704cf57 (diff) | |
| download | perlweeklychallenge-club-3b50cfa06f83676fcac4fa665c06951be6e8446e.tar.gz perlweeklychallenge-club-3b50cfa06f83676fcac4fa665c06951be6e8446e.tar.bz2 perlweeklychallenge-club-3b50cfa06f83676fcac4fa665c06951be6e8446e.zip | |
Solutions to challenge 031 problem 1 and 2 in Perl 6
| -rw-r--r-- | challenge-031/noud/perl6/ch1.p6 | 39 | ||||
| -rw-r--r-- | challenge-031/noud/perl6/ch2.p6 | 14 |
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}; +} |
