aboutsummaryrefslogtreecommitdiff
path: root/challenge-142/jo-37
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-12-06 13:21:43 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2021-12-08 19:38:15 +0100
commitdb3b628498be8e9548a5f36536ac72cd8fc76339 (patch)
treecccbbcd5e44302b9a7a969ec3d8002a907551954 /challenge-142/jo-37
parentef93fd69449a184b5681415853c4272bb85bf384 (diff)
downloadperlweeklychallenge-club-db3b628498be8e9548a5f36536ac72cd8fc76339.tar.gz
perlweeklychallenge-club-db3b628498be8e9548a5f36536ac72cd8fc76339.tar.bz2
perlweeklychallenge-club-db3b628498be8e9548a5f36536ac72cd8fc76339.zip
Solution to task 1
Diffstat (limited to 'challenge-142/jo-37')
-rwxr-xr-xchallenge-142/jo-37/perl/ch-1.pl64
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-142/jo-37/perl/ch-1.pl b/challenge-142/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..fb16b8073d
--- /dev/null
+++ b/challenge-142/jo-37/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Math::Prime::Util 'divisors';
+use experimental 'signatures';
+
+our ($tests, $examples, $base);
+$base //= 10;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [-base=B] [M N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-base=B
+ Use base B. Default: 10
+
+M N
+ count the divisors of M having N as last digit in base B.
+
+EOS
+
+
+### Input and Output
+
+say num_div_mod(@ARGV, $base);
+
+
+### Implementation
+
+# Generalizing the task by taking "the last digit" in a given base.
+sub num_div_mod ($m, $n, $base=10) {
+ scalar grep $_ % $base == $n, divisors $m
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is num_div_mod(24, 2), 2, 'example 1';
+ is num_div_mod(30, 5), 2, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is num_div_mod(9, 1, 2), 3, 'odd divisors of 9';
+ is num_div_mod(8, 0, 2), 3, 'even divisors of 8';
+ is num_div_mod(24, 0, 1), 8, 'all divisors of 24';
+ }
+
+ done_testing;
+ exit;
+}