diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-04 16:25:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-04 16:25:34 +0100 |
| commit | cc85651a708f4c7d712435db8deeb3d54673a1a5 (patch) | |
| tree | f4236c5190d220d4655bae568525f936a8f29a9d | |
| parent | 22932316886ffd2c76f615c9fd38ddcb93de8d1d (diff) | |
| parent | 87b6e596991b8bebbf362cc53336c0535b465e78 (diff) | |
| download | perlweeklychallenge-club-cc85651a708f4c7d712435db8deeb3d54673a1a5.tar.gz perlweeklychallenge-club-cc85651a708f4c7d712435db8deeb3d54673a1a5.tar.bz2 perlweeklychallenge-club-cc85651a708f4c7d712435db8deeb3d54673a1a5.zip | |
Merge pull request #2030 from choroba/ech072
Solve 072 (Trailing Zeros and Lines Range) by E. Choroba
| -rwxr-xr-x | challenge-072/e-choroba/perl5/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-072/e-choroba/perl5/ch-2.pl | 9 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-072/e-choroba/perl5/ch-1.pl b/challenge-072/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..fe99c47555 --- /dev/null +++ b/challenge-072/e-choroba/perl5/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Math::BigInt; + +sub factorial { + my ($n) = @_; + my $p = 'Math::BigInt'->new(1); + $p *= $_ for 1 .. $n; + $p +} + +sub trailing0s { + my ($n) = @_; + length((factorial($n) =~ /(0*)$/)[0]) +} + +sub trailing0s_fast { + my ($n) = @_; + my $z = 0; + my $i = 1; + $z += int($n / ($i *= 5)) while $i < $n; + return $z +} + +use Test::More; + +subtest factorial => sub { + is factorial(0), 1; + is factorial(1), 1; + is factorial(5), 120; +}; + +subtest trailing0s => sub { + for my $n (0 .. 1000) { + is trailing0s($n), trailing0s_fast($n), "fast $n"; + } +}; + +done_testing(); + +use Benchmark qw{ cmpthese }; +cmpthese(-3, { + slow => 'trailing0s(2000)', + fast => 'trailing0s_fast(2000)', +}); diff --git a/challenge-072/e-choroba/perl5/ch-2.pl b/challenge-072/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..fbefa6b8be --- /dev/null +++ b/challenge-072/e-choroba/perl5/ch-2.pl @@ -0,0 +1,9 @@ +#!/usr/bin/perl +use warnings; +use strict; + +my ($from, $to) = (shift, shift); +while (<>) { + print if ($. == $from) .. ($. == $to); +} + |
