aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-08-04 18:37:38 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-08-04 18:37:38 +0200
commit6720854f08986dbfeceebab9125f85a72ccc047c (patch)
tree333f743d42870dbcb5256da5fe788f2d91b5ba9c
parentb823934884e49f14b33c59467b0af0fbfe371f70 (diff)
parent77ecfbb261970d6becc0874f75fefc92a9c0af4d (diff)
downloadperlweeklychallenge-club-6720854f08986dbfeceebab9125f85a72ccc047c.tar.gz
perlweeklychallenge-club-6720854f08986dbfeceebab9125f85a72ccc047c.tar.bz2
perlweeklychallenge-club-6720854f08986dbfeceebab9125f85a72ccc047c.zip
Solutions for challenge 072
-rwxr-xr-xchallenge-072/jo-37/perl/ch-1.pl38
-rw-r--r--challenge-072/jo-37/perl/ch-2.in8
-rwxr-xr-xchallenge-072/jo-37/perl/ch-2.pl21
3 files changed, 67 insertions, 0 deletions
diff --git a/challenge-072/jo-37/perl/ch-1.pl b/challenge-072/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..71fd999c68
--- /dev/null
+++ b/challenge-072/jo-37/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+use v5.14;
+use Test2::V0;
+
+# First off: The limitation to N <= 10 kills all joy with this task.
+# I'm going to ignore the restriction.
+#
+# Every trailing zero in any decimal number is produced by a pair of
+# factors 2 and 5. As there are many more factors 2 in a factorial
+# than factors 5, the number of factors 5 give the number of
+# trailing zeroes for the factorial.
+# Every multiple of 5 in the product 2 * 3 * ... * n gives at least
+# one such factor. Higher powers of 5 give one additional factor.
+# In summary, the number of trailing zeroes in a factorial is sum of
+# the number of multiples of every power of 5 in 2 .. n.
+sub fac_trailing_zeroes {
+ my ($n, $tz) = (shift, 0);
+ for (my $f = 5; $f <= $n; $f *= 5) {
+ $tz += int $n / $f;
+ }
+ $tz;
+}
+
+is fac_trailing_zeroes(10), 2, 'first example';
+is fac_trailing_zeroes(7), 1, 'second example';
+is fac_trailing_zeroes(4), 0, 'third example';
+
+my $tz100 = do {
+ use bigint;
+ length +(100 + 0)->bfac =~ s/.*?(?=0*$)//r;
+};
+say "100! has $tz100 trailing zeroes";
+is fac_trailing_zeroes(100), $tz100, 'cross-check 100!';
+
+say "1000000! has ", fac_trailing_zeroes(1000000), " trailing zeroes";
+
+done_testing;
diff --git a/challenge-072/jo-37/perl/ch-2.in b/challenge-072/jo-37/perl/ch-2.in
new file mode 100644
index 0000000000..a694c02976
--- /dev/null
+++ b/challenge-072/jo-37/perl/ch-2.in
@@ -0,0 +1,8 @@
+L1
+L2
+L3
+L4
+L5
+L6
+L7
+L8
diff --git a/challenge-072/jo-37/perl/ch-2.pl b/challenge-072/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..423ed6ca80
--- /dev/null
+++ b/challenge-072/jo-37/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+sub print_range {
+ my ($file, $A, $B) = (shift, shift() + 0, shift() + 0);
+
+ return if !$file || $A < 1 || $B < $A;
+
+ open my $fh, '<', $file or die "$file: $!\n";
+ eval <<EOS;
+ while (<\$fh>) {
+ print if $A .. $B;
+ }
+EOS
+ die $@ if $@;
+ close $fh or warn "$file: $!\n";
+}
+
+print_range 'ch-2.in', 3, 5;