diff options
| author | Jan Krňávek <Jan.Krnavek@gmail.com> | 2020-08-04 14:07:40 +0200 |
|---|---|---|
| committer | Jan Krňávek <Jan.Krnavek@gmail.com> | 2020-08-04 14:17:36 +0200 |
| commit | f91b01ef6fae9da3ff25c619d5c152cac1ddc8dd (patch) | |
| tree | 9ef5e0fc8c7f0041e794162f863a1e4ffbc29698 | |
| parent | 05468f0bc7099d7288fb4107b77d86e4ebeb8a52 (diff) | |
| download | perlweeklychallenge-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.raku | 33 |
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; +} |
