diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-31 12:17:26 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-31 12:17:26 +0100 |
| commit | ea0c090585d87f5a248d0755d846d6ab68a6f0e6 (patch) | |
| tree | cd77b01773c1a2d0016926799a34847bcc45c645 | |
| parent | 4186b37af5fbdbfe6ba5754ba8eb687682e45f6c (diff) | |
| download | perlweeklychallenge-club-ea0c090585d87f5a248d0755d846d6ab68a6f0e6.tar.gz perlweeklychallenge-club-ea0c090585d87f5a248d0755d846d6ab68a6f0e6.tar.bz2 perlweeklychallenge-club-ea0c090585d87f5a248d0755d846d6ab68a6f0e6.zip | |
Add Perl solution
| -rw-r--r-- | challenge-176/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-176/paulo-custodio/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-176/paulo-custodio/perl/ch-2.pl | 45 | ||||
| -rw-r--r-- | challenge-176/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-176/paulo-custodio/t/test-2.yaml | 5 |
5 files changed, 108 insertions, 0 deletions
diff --git a/challenge-176/paulo-custodio/Makefile b/challenge-176/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-176/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-176/paulo-custodio/perl/ch-1.pl b/challenge-176/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e17913aeae --- /dev/null +++ b/challenge-176/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +# Challenge 176 +# +# Task 1: Permuted Multiples +# Submitted by: Mohammad S Anwar +# +# Write a script to find the smallest positive integer x such that x, 2x, 3x, +# 4x, 5x and 6x are permuted multiples of each other. +# +# For example, the integers 125874 and 251748 are permutated multiples of each +# other as +# +# 251784 = 2 x 125874 +# +# and also both have the same digits but in different order. +# +# Output +# +# 142857 + +use Modern::Perl; + +sub is_permuted { + my($a, $b) = @_; + return join('', sort split //, $a) eq join('', sort split //, $b); +} + +sub is_permuted_multiple { + my($n, $k) = @_; + return is_permuted($n, $n*$k); +} + +sub is_permuted_multiples { + my($n) = @_; + return is_permuted_multiple($n, 2) && + is_permuted_multiple($n, 3) && + is_permuted_multiple($n, 4) && + is_permuted_multiple($n, 5) && + is_permuted_multiple($n, 6); +} + +sub smallest_permuted_multiples { + my $n = 1; + while (1) { + return $n if is_permuted_multiples($n); + $n++; + } +} + +say smallest_permuted_multiples(); diff --git a/challenge-176/paulo-custodio/perl/ch-2.pl b/challenge-176/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..cd0a4e69c5 --- /dev/null +++ b/challenge-176/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Challenge 176 +# +# Task 2: Reversible Numbers +# Submitted by: Mohammad S Anwar +# +# Write a script to find out all Reversible Numbers below 100. +# +# A number is said to be a reversible if sum of the number and its reverse +# had only odd digits. +# +# For example, +# +# 36 is reversible number as 36 + 63 = 99 i.e. all digits are odd. +# 17 is not reversible as 17 + 71 = 88, none of the digits are odd. +# +# Output +# +# 10, 12, 14, 16, 18, 21, 23, 25, 27, +# 30, 32, 34, 36, 41, 43, 45, 50, 52, +# 54, 61, 63, 70, 72, 81, 90 + +use Modern::Perl; + +sub is_reversible { + my($n) = @_; + my $rev = join '', reverse split //, $n; + return ($n+$rev) =~ /^[13579]+$/; +} + +sub reversibles_up_to { + my($N) = @_; + my $n = 1; + my @result; + while ($n < $N) { + push @result, $n if is_reversible($n); + $n++; + } + return @result; +} + +@ARGV==1 or die "usage: ch-2.pl N\n"; +my $N = shift; +say join ", ", reversibles_up_to($N); diff --git a/challenge-176/paulo-custodio/t/test-1.yaml b/challenge-176/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..e24ea243e9 --- /dev/null +++ b/challenge-176/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 142857 diff --git a/challenge-176/paulo-custodio/t/test-2.yaml b/challenge-176/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b4aeece491 --- /dev/null +++ b/challenge-176/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 100 + input: + output: 10, 12, 14, 16, 18, 21, 23, 25, 27, 30, 32, 34, 36, 41, 43, 45, 50, 52, 54, 61, 63, 70, 72, 81, 90 |
