aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-02 12:06:37 +0100
committerGitHub <noreply@github.com>2021-08-02 12:06:37 +0100
commit698c82d1451b65c4f5a44a17dee19b43acb109bd (patch)
tree12409e563022c406c792d7680803e8445d46b574
parent07cf10cea7d97f529a19e00ea9a1f902fc74177e (diff)
parent16dca877e7a21f4abe938ffc1b39a2f701d8e549 (diff)
downloadperlweeklychallenge-club-698c82d1451b65c4f5a44a17dee19b43acb109bd.tar.gz
perlweeklychallenge-club-698c82d1451b65c4f5a44a17dee19b43acb109bd.tar.bz2
perlweeklychallenge-club-698c82d1451b65c4f5a44a17dee19b43acb109bd.zip
Merge pull request #4649 from kjetillll/kjetillll-ugly-numbers
my contrib
-rwxr-xr-xchallenge-123/kjetillll/perl/ch-1.pl30
-rw-r--r--challenge-123/kjetillll/perl/ch-2.pl25
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-123/kjetillll/perl/ch-1.pl b/challenge-123/kjetillll/perl/ch-1.pl
new file mode 100755
index 0000000000..fcede75fdc
--- /dev/null
+++ b/challenge-123/kjetillll/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+# Prints the Nth ugly number given by the first command line argument.
+# An ugly number is an integer factorized only by 2, 3 and 5s and no
+# other primes where 1 is considered the first ugly number.
+#
+# sub is_ugly return 1 (true) for ugly integers and 0 (false) otherwise.
+# sub nth_ugly returns the Nth ugly number by recursion.
+#
+# Linux command line test:
+# for n in {1..30}; do echo -n "$n "; perl ch-1.pl $n; done
+# Outputs the first 30 which is:
+# 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 45 48 50 54 60 64 72 75 80
+
+sub is_ugly {
+ my $n=shift;
+ $n != int$n ? 0
+ :$n == 1 ? 1
+ :( grep is_ugly($n/$_),2,3,5 ) ? 1
+ : 0
+}
+
+sub nth_ugly {
+ my($n,$try)=(@_,0);
+ $n ? nth_ugly( $n - is_ugly(++$try), $try) : $try
+}
+
+my($N)=@ARGV;
+
+print nth_ugly( $N ), "\n";
diff --git a/challenge-123/kjetillll/perl/ch-2.pl b/challenge-123/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..c3b8be3be3
--- /dev/null
+++ b/challenge-123/kjetillll/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+
+#Returns true if four points form a square.
+
+# is_square( [10,20], [20,20], [20,10], [10,10] ) #return 1 (true)
+# is_square( [2,6], [5,1], [0,-2], [-3,3] ) #return 1 (true)
+
+# perl ch-2.pl 2,6 5,1 0,-2 -3,3 #prints 1
+
+sub is_square {
+ die if @_ != 4 or grep@$_!=2,@_;
+ my %dsf; #dsf=distance squared found
+ for my $p1 ( 0 .. 2 ){ # for the three first points
+ for my $p2 ( $p1+1 .. 3 ){ # loop through the rest of the points
+ my($p1x,$p1y,$p2x,$p2y)=(@{$_[$p1]},@{$_[$p2]});
+ $dsf{ ($p1x-$p2x)**2 + ($p1y-$p2y)**2 }++; #register distance squared
+ }}
+ #If only two different distances found between any two points it means
+ #they form a square, returns 1 (true) if so, or 0 (false) otherwise.
+ #Counting distances squared saves having to do square root as in Pythagoras
+ #grep > 0 prevents two pairs of duplicate points or four equal points from returning true
+ 2==(grep$_>0,keys%dsf) ? 1 : 0
+}
+
+print is_square( map[split/,/],@ARGV ), "\n";