diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-01 18:59:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-01 18:59:27 +0100 |
| commit | 3d908a3fe9d905f5ae43e4fe5b756140dc600f75 (patch) | |
| tree | eb714c8b37da20f1d4f79353970bc2e033537acb | |
| parent | 5f8333fec725cf031970bf676fa86aa532e4e23e (diff) | |
| parent | f0b0f5aad61ba8f39e7556d8c23ced2576f00cee (diff) | |
| download | perlweeklychallenge-club-3d908a3fe9d905f5ae43e4fe5b756140dc600f75.tar.gz perlweeklychallenge-club-3d908a3fe9d905f5ae43e4fe5b756140dc600f75.tar.bz2 perlweeklychallenge-club-3d908a3fe9d905f5ae43e4fe5b756140dc600f75.zip | |
Merge pull request #4639 from wanderdoc/master
Solutions to challenge-123
| -rw-r--r-- | challenge-123/wanderdoc/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-123/wanderdoc/perl/ch-2.pl | 64 |
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-123/wanderdoc/perl/ch-1.pl b/challenge-123/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..f2c9b71366 --- /dev/null +++ b/challenge-123/wanderdoc/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an integer $n >= 1. Write a script to find the $nth element of Ugly Numbers. +Ugly numbers are those number whose prime factors are 2, 3 or 5. For example, the first 10 Ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. +Example +Input: $n = 7 Output: 8 +Input: $n = 10 Output: 12 +=cut + + + + + + + +use List::Util qw(min); + +my $N = shift or die "Number?$/"; +my $idx2 = my $idx3 = my $idx5 = 1; +my @num; + +my $i = 1; + +$num[$i] = 1; + +while ( $i < $N ) +{ + my $last = $num[$i]; + $idx2++ while $num[$idx2] * 2 <= $last; + $idx3++ while $num[$idx3] * 3 <= $last; + $idx5++ while $num[$idx5] * 5 <= $last; + $i++; + $num[$i] = min($num[$idx2] * 2, $num[$idx3] * 3, $num[$idx5] * 5); + +} + + +print $num[$i], $/;
\ No newline at end of file diff --git a/challenge-123/wanderdoc/perl/ch-2.pl b/challenge-123/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..6e3fc6983f --- /dev/null +++ b/challenge-123/wanderdoc/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given coordinates of four points i.e. (x1, y1), (x2, y2), (x3, y3) and (x4, y4). +Write a script to find out if the given four points form a square. +=cut + + + + + + + + + + +use Struct::Dumb; +struct Point => [qw( x y )]; + +sub check_square +{ + my @points = @_; + my %distances; + + for my $i ( 0 .. 3 ) + { + for my $j ( 1 .. 3 ) + { + next if $i >= $j; + $distances{$i}{$j} = calc_distance(@points[$i, $j]); + return 0 if 0 == $distances{$i}{$j}; + } + } + + return 1 + if + + + ( + ($distances{0}{1} == $distances{0}{2} and $distances{0}{3} == 2 * $distances{0}{1} and $distances{0}{1} == $distances{2}{3}) + or + ($distances{0}{1} == $distances{0}{3} and $distances{0}{2} == 2 * $distances{0}{1} and $distances{0}{1} == $distances{2}{3}) + or + ($distances{1}{2} == $distances{1}{3} and $distances{0}{1} == 2 * $distances{1}{2} and $distances{1}{2} == $distances{0}{3}) + ); + return 0; + +} + +sub calc_distance # distance _squared_. +{ + my @pts = @_; + return (($pts[0]->x - $pts[1]->x) * ($pts[0]->x - $pts[1]->x) + + ($pts[0]->y - $pts[1]->y) * ($pts[0]->y - $pts[1]->y)); +} + + +print check_square(Point(10, 20), Point(20, 20), Point(20, 10), Point(10, 10)), $/; # 1 +print check_square(Point(12, 24), Point(16, 10), Point(20, 12), Point(18, 16)), $/; # 0 +print check_square(Point(10, 30), Point(20, 10), Point(30, 40), Point(40, 20)), $/; # 1 +print check_square(Point(-2, -1), Point(-2, 2), Point( 1, -1), Point( 1, 2)), $/; # 1 +print check_square(Point(10, 10), Point(10, 20), Point(30, 10), Point(30, 20)), $/; # 0
\ No newline at end of file |
