From 1a8f59f1e435ed4845cee529fc7d0d3c0608456c Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 31 Mar 2023 13:06:41 +0100 Subject: Add Perl solution --- challenge-177/paulo-custodio/Makefile | 2 + challenge-177/paulo-custodio/perl/ch-1.pl | 50 ++++++++++++++++++++++ challenge-177/paulo-custodio/perl/ch-2.pl | 66 ++++++++++++++++++++++++++++++ challenge-177/paulo-custodio/t/test-1.yaml | 10 +++++ challenge-177/paulo-custodio/t/test-2.yaml | 5 +++ 5 files changed, 133 insertions(+) create mode 100644 challenge-177/paulo-custodio/Makefile create mode 100644 challenge-177/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-177/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-177/paulo-custodio/t/test-1.yaml create mode 100644 challenge-177/paulo-custodio/t/test-2.yaml diff --git a/challenge-177/paulo-custodio/Makefile b/challenge-177/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-177/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-177/paulo-custodio/perl/ch-1.pl b/challenge-177/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..4eb45f05ce --- /dev/null +++ b/challenge-177/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Challenge 177 +# +# Task 1: Damm Algorithm +# Submitted by: Mohammad S Anwar +# +# You are given a positive number, $n. +# +# Write a script to validate the given number against the included check digit. +# +# Please checkout the wikipedia page for information. +# Example 1 +# +# Input: $n = 5724 +# Output: 1 as it is valid number +# +# Example 2 +# +# Input: $n = 5727 +# Output: 0 as it is invalid number + +use Modern::Perl; + +# https://en.wikipedia.org/wiki/Damm_algorithm + +my @table = ( + [qw( 0 3 1 7 5 9 8 6 4 2 )], + [qw( 7 0 9 2 1 5 4 8 6 3 )], + [qw( 4 2 0 6 8 7 1 3 5 9 )], + [qw( 1 7 5 0 9 8 3 4 2 6 )], + [qw( 6 1 2 3 0 4 5 9 7 8 )], + [qw( 3 6 7 4 2 0 9 5 8 1 )], + [qw( 5 8 6 9 7 2 0 1 3 4 )], + [qw( 8 9 4 5 3 6 2 0 1 7 )], + [qw( 9 4 3 8 6 1 7 2 0 5 )], + [qw( 2 5 8 1 4 3 6 7 9 0 )], +); + +sub validate { + my($n) = @_; + my $digit = 0; + for (split //, $n) { + $digit = $table[$digit][$_]; + } + return $digit==0 ? 1 : 0; +} + +@ARGV==1 or die "usage: ch-2.pl number\n"; +say validate(shift); diff --git a/challenge-177/paulo-custodio/perl/ch-2.pl b/challenge-177/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..a128c71570 --- /dev/null +++ b/challenge-177/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl + +# Challenge 177 +# +# Task 2: Palindromic Prime Cyclops +# Submitted by: Mohammad S Anwar +# +# Write a script to generate first 20 Palindromic Prime Cyclops Numbers. +# +# A cyclops number is a number with an odd number of digits that has a zero +# in the center only. +# +# +# Output +# +# 101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049, +# 1120211, 1150511, 1160611, 1180811, 1190911, 1250521, 1280821, +# 1360631, 1390931, 1490941, 1520251 + +use Modern::Perl; + +sub is_palindromic { + my($n) = @_; + my $rev = join '', reverse split //, $n; + return $n == $rev; +} + +sub is_cyclops { + my($n) = @_; + return 0 unless $n =~ /^([1-9]+)0([1-9]+)$/; + return 0 unless length($1)==length($2); + return 1; +} + +sub is_prime { + my($n) = @_; + return 0 if $n <= 1; + return 1 if $n <= 3; + return 0 if ($n % 2)==0 || ($n % 3)==0; + for (my $i = 5; $i*$i <= $n; $i += 6) { + return 0 if ($n % $i)==0 || ($n % ($i+2))==0; + } + return 1; +} + +sub is_palindromic_cyclops_prime { + my($n) = @_; + return is_palindromic($n) && is_cyclops($n) && is_prime($n); +} + +sub palindromic_cyclops_prime { + my($N) = @_; + my $n = 100; + my @result; + while (@result < $N) { + push @result, $n if is_palindromic_cyclops_prime($n); + $n++; + } + return @result; +} + +@ARGV==1 or die "usage: ch-2.pl N\n"; +my $N = shift; +say join ", ", palindromic_cyclops_prime($N); + + diff --git a/challenge-177/paulo-custodio/t/test-1.yaml b/challenge-177/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..cdcc1776dc --- /dev/null +++ b/challenge-177/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 5724 + input: + output: 1 +- setup: + cleanup: + args: 5724 + input: + output: 1 diff --git a/challenge-177/paulo-custodio/t/test-2.yaml b/challenge-177/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..0b54d9c382 --- /dev/null +++ b/challenge-177/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 20 + input: + output: 101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049, 1120211, 1150511, 1160611, 1180811, 1190911, 1250521, 1280821, 1360631, 1390931, 1490941, 1520251 -- cgit