aboutsummaryrefslogtreecommitdiff
path: root/challenge-049/e-choroba
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-25 11:18:11 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-25 11:18:11 +0000
commit82d6696419db53bb008faadf5de6df30c64f3cc3 (patch)
tree533a26b86a8b2539bf743637365506ec43fc4d0b /challenge-049/e-choroba
parent2c5c35db84a34508088bf78eb9db621f6cfd9d3d (diff)
downloadperlweeklychallenge-club-82d6696419db53bb008faadf5de6df30c64f3cc3.tar.gz
perlweeklychallenge-club-82d6696419db53bb008faadf5de6df30c64f3cc3.tar.bz2
perlweeklychallenge-club-82d6696419db53bb008faadf5de6df30c64f3cc3.zip
- Added solutions by E. Choroba.
Diffstat (limited to 'challenge-049/e-choroba')
-rwxr-xr-xchallenge-049/e-choroba/perl/ch-1.pl14
-rwxr-xr-xchallenge-049/e-choroba/perl/ch-1a.pl20
-rwxr-xr-xchallenge-049/e-choroba/perl/ch-1b.pl28
3 files changed, 31 insertions, 31 deletions
diff --git a/challenge-049/e-choroba/perl/ch-1.pl b/challenge-049/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..fe8f988745
--- /dev/null
+++ b/challenge-049/e-choroba/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+my $x = shift;
+say smallest_multiple($x);
+
+sub smallest_multiple {
+ my ($n) = @_;
+ my $r = $n;
+ $r += $n until $r =~ /^[01]+$/;
+ $r
+}
diff --git a/challenge-049/e-choroba/perl/ch-1a.pl b/challenge-049/e-choroba/perl/ch-1a.pl
index fe8f988745..84fc5c711c 100755
--- a/challenge-049/e-choroba/perl/ch-1a.pl
+++ b/challenge-049/e-choroba/perl/ch-1a.pl
@@ -8,7 +8,21 @@ say smallest_multiple($x);
sub smallest_multiple {
my ($n) = @_;
- my $r = $n;
- $r += $n until $r =~ /^[01]+$/;
- $r
+ return 0 unless $n;
+
+ my $binary = 1 . (0 x (length($n) - 1));
+ increment($binary) while $binary % $n;
+ $binary
+}
+
+sub increment {
+ my $pos = rindex $_[0], 0;
+ if ($pos > -1) {
+ substr $_[0], $pos, 1, '1';
+ substr $_[0], $pos + 1, length($_[0]) - $pos - 1,
+ '0' x (length($_[0]) - $pos - 1);
+ } else {
+ $_[0] = '1' . ('0' x length $_[0]);
+ }
}
+
diff --git a/challenge-049/e-choroba/perl/ch-1b.pl b/challenge-049/e-choroba/perl/ch-1b.pl
deleted file mode 100755
index 84fc5c711c..0000000000
--- a/challenge-049/e-choroba/perl/ch-1b.pl
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/usr/bin/perl
-use warnings;
-use strict;
-use feature qw{ say };
-
-my $x = shift;
-say smallest_multiple($x);
-
-sub smallest_multiple {
- my ($n) = @_;
- return 0 unless $n;
-
- my $binary = 1 . (0 x (length($n) - 1));
- increment($binary) while $binary % $n;
- $binary
-}
-
-sub increment {
- my $pos = rindex $_[0], 0;
- if ($pos > -1) {
- substr $_[0], $pos, 1, '1';
- substr $_[0], $pos + 1, length($_[0]) - $pos - 1,
- '0' x (length($_[0]) - $pos - 1);
- } else {
- $_[0] = '1' . ('0' x length $_[0]);
- }
-}
-