aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2020-08-03 18:45:04 -0400
committerWalt Mankowski <waltman@pobox.com>2020-08-03 18:45:04 -0400
commitbef2073f00f2525441571a005c640a9cf966ffad (patch)
treefb82c81fd024eb644f60393aec951add47271702
parent97131b3313551350f7c7a073d7ad401a1db9d23f (diff)
downloadperlweeklychallenge-club-bef2073f00f2525441571a005c640a9cf966ffad.tar.gz
perlweeklychallenge-club-bef2073f00f2525441571a005c640a9cf966ffad.tar.bz2
perlweeklychallenge-club-bef2073f00f2525441571a005c640a9cf966ffad.zip
perl code for challenge 72 task 1
initial draft
-rw-r--r--challenge-072/walt-mankowski/perl/ch-1.pl45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-072/walt-mankowski/perl/ch-1.pl b/challenge-072/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..dc4efbd0fd
--- /dev/null
+++ b/challenge-072/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+
+# TASK #1 › Trailing Zeroes
+# Submitted by: Mohammad S Anwar
+#
+# You are given a positive integer $N (<= 10).
+#
+# Write a script to print number of trailing zeroes in $N!.
+# Example 1
+# Input: $N = 10
+# Output: 2 as $N! = 3628800 has 2 trailing zeroes
+#
+# Example 2
+# Input: $N = 7
+# Output: 1 as $N! = 5040 has 1 trailing zero
+#
+# Example 3
+# Input: $N = 4
+# Output: 0 as $N! = 24 has 0 trailing zero
+
+sub fact($n) {
+ my $res = 1;
+ $res *= $_ for 2..$n;
+ return $res;
+}
+
+sub num_trailing_zeros($n) {
+ my $cnt = 0;
+ my $pwr = 10;
+ while ($n % $pwr == 0) {
+ $cnt++;
+ $pwr *= 10;
+ }
+ return $cnt;
+}
+
+my $n = shift @ARGV;
+my $f = fact($n);
+my $z = num_trailing_zeros($f);
+say "$n as \$N! = $f has $z trailing zero(s)";
+