aboutsummaryrefslogtreecommitdiff
path: root/challenge-105
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-28 20:32:34 +0100
committerGitHub <noreply@github.com>2021-03-28 20:32:34 +0100
commitb44b6417dbbfdcc0b39e55e50d6829075a3908e3 (patch)
tree2d0516e09f25946015905e2c208cbca9117d89cb /challenge-105
parent8303f318d4103fb30cda35f7f21afca35acd5051 (diff)
parent1a1a52ed2a7b20a6f7c14f3ace3f46b1901d3faa (diff)
downloadperlweeklychallenge-club-b44b6417dbbfdcc0b39e55e50d6829075a3908e3.tar.gz
perlweeklychallenge-club-b44b6417dbbfdcc0b39e55e50d6829075a3908e3.tar.bz2
perlweeklychallenge-club-b44b6417dbbfdcc0b39e55e50d6829075a3908e3.zip
Merge pull request #3788 from E7-87-83/master
Challenge 105 Task 1
Diffstat (limited to 'challenge-105')
-rw-r--r--challenge-105/cheok-yin-fung/perl/ch-1.pl78
1 files changed, 78 insertions, 0 deletions
diff --git a/challenge-105/cheok-yin-fung/perl/ch-1.pl b/challenge-105/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..3a8ab8c2f5
--- /dev/null
+++ b/challenge-105/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/perl
+# The Weekly Challenge 105
+# Task 1: Nth Root
+# Usage: ch-1.pl $N $k
+
+# $N positive integer, $k positive number
+# Accuracy up to 2 decimal places
+
+use strict;
+use warnings;
+
+my $pow = 1;
+my $N = int $ARGV[0];
+my $k = $ARGV[1];
+
+print "WARN: Recommend to take the result from Newton's method if two methods dispute";
+print "WARN: N is large; probably dispute between two methods \n"
+ if $N > 9; # parameter chosen by testing
+print "WARN: N is too large; probably inaccurate result(s) \n"
+ if $N > 100;
+print "WARN: k is too small against N; probably output errors \n"
+ if $k/$N < 0.05;
+
+
+sub lazy_root {
+# I like math, but being ultralazy,
+# thought of settling with the task by junior school math
+ my $puppet_k = $k * (100**$N);
+ my $i = 0;
+
+ while ($pow < $puppet_k) {
+ $i++;
+ $pow = $i**$N;
+ }
+
+ if ($pow > $puppet_k) {
+ my $pow_smaller = ($i-1)**$N;
+ $i = $i-1 if ($pow - $puppet_k) > ($puppet_k - $pow_smaller);
+ }
+
+ $i = $i/100.0;
+
+ if ($pow == $puppet_k) {
+ print "$i\n";
+ }
+ else {
+ printf "%.2f\n",$i;
+ }
+}
+
+sub newton_root {
+ my @x;
+ $x[0] = $N > 20 ? sqrt($k)/$N : sqrt($k);
+ my $dff = 0.0005; # error terms computable
+ # but too lazy to check Numerical Analysis textbook
+ my $t = 0;
+ while ($dff >= 0.0005) {
+ $dff = $x[$t]**$N - $k;
+ $x[$t+1] = $x[$t] - ( $dff / $x[$t]**($N-1) / $N);
+ $t++;
+ }
+
+ my $puppet_x = sprintf "%.2f", $x[$t];
+ if ($puppet_x**$N==$k) {
+ $puppet_x =~ s/\.([0-9])*0$/\.$1/;
+ $puppet_x =~ s/\.0$//;
+ print "$puppet_x\n";
+ }
+ else {
+ printf "%.2f\n", $x[$t];
+ }
+}
+
+print "By lazy method:\n";
+lazy_root();
+
+print "By Newton's method:\n";
+newton_root();