aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-15 19:17:32 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-15 19:17:32 +0100
commitb658b63de57d6dafea54b9d5d9f311f9d22ba9ed (patch)
tree6f4f9c12c0e1cca2824a713c4fc28246919fb578
parent7efb373bb9adffa79f84825217015835805298b5 (diff)
downloadperlweeklychallenge-club-b658b63de57d6dafea54b9d5d9f311f9d22ba9ed.tar.gz
perlweeklychallenge-club-b658b63de57d6dafea54b9d5d9f311f9d22ba9ed.tar.bz2
perlweeklychallenge-club-b658b63de57d6dafea54b9d5d9f311f9d22ba9ed.zip
Solution to task 1
-rwxr-xr-xchallenge-252/jo-37/perl/ch-1.pl58
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-252/jo-37/perl/ch-1.pl b/challenge-252/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..0b1cc1f5c2
--- /dev/null
+++ b/challenge-252/jo-37/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+use Math::Prime::Util 'divisors';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [I...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+I...
+ list of integers
+
+EOS
+
+
+### Input and Output
+
+say snss(@ARGV);
+
+
+### Implementation
+
+sub snss {
+ sum pdl(@_)->(indx(divisors @_) - 1) ** 2
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is snss(1, 2, 3, 4), 21, 'example 1';
+ is snss(2, 7, 1, 19, 18, 3), 63, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is snss(-1, -2), 5, 'negative values';
+ }
+
+ done_testing;
+ exit;
+}