aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Krňávek <Jan.Krnavek@gmail.com>2020-08-04 14:07:40 +0200
committerJan Krňávek <Jan.Krnavek@gmail.com>2020-08-04 14:17:36 +0200
commitf91b01ef6fae9da3ff25c619d5c152cac1ddc8dd (patch)
tree9ef5e0fc8c7f0041e794162f863a1e4ffbc29698
parent05468f0bc7099d7288fb4107b77d86e4ebeb8a52 (diff)
downloadperlweeklychallenge-club-f91b01ef6fae9da3ff25c619d5c152cac1ddc8dd.tar.gz
perlweeklychallenge-club-f91b01ef6fae9da3ff25c619d5c152cac1ddc8dd.tar.bz2
perlweeklychallenge-club-f91b01ef6fae9da3ff25c619d5c152cac1ddc8dd.zip
Challenge 072-1
-rw-r--r--challenge-072/wambash/raku/ch-1.raku33
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-072/wambash/raku/ch-1.raku b/challenge-072/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..52b915d5cd
--- /dev/null
+++ b/challenge-072/wambash/raku/ch-1.raku
@@ -0,0 +1,33 @@
+use v6;
+constant @fact = 1, |[\*] 1..* ;
+subset BigInt of Int where * ≥ 2000;
+
+multi trailing-zeroes ( $n ) {
+ @fact[$n].match(/0*$/).chars
+}
+
+multi trailing-zeroes ( BigInt $n ) {
+ trailing-zeroes-of-bigint($n)
+}
+
+sub trailing-zeroes-of-bigint ( $n ) {
+ state @coeficient = [\+] 0, {5**$++} ... *;
+ [+] $n.polymod(5 xx *) Z* @coeficient
+}
+
+multi MAIN ($n) {
+ my $fact = $n ~~ BigInt ?? "$n!" !! "$n! = @fact[$n]";
+ say "$fact has {trailing-zeroes($n)} trailing zeroes"
+}
+
+multi MAIN (:$test! ) {
+ use Test;
+ is trailing-zeroes(10), 2;
+ is trailing-zeroes(7), 1;
+ is trailing-zeroes(4), 0;
+ is trailing-zeroes-of-bigint(10), 2;
+ is trailing-zeroes-of-bigint(7), 1;
+ is trailing-zeroes-of-bigint(4), 0;
+ is trailing-zeroes-of-bigint($_), trailing-zeroes($_) for ^2000 .pick(20);
+ done-testing;
+}