aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2024-01-15 22:29:52 +0800
committerCY Fung <fungcheokyin@gmail.com>2024-01-15 22:29:52 +0800
commit73704e823a01f00d461dac41dfae852471186d34 (patch)
tree17b39542b54fc194d3b50335cca8e4e0cfecfb32
parent9ffb043031b2e46225810346f673534960b3f7cc (diff)
downloadperlweeklychallenge-club-73704e823a01f00d461dac41dfae852471186d34.tar.gz
perlweeklychallenge-club-73704e823a01f00d461dac41dfae852471186d34.tar.bz2
perlweeklychallenge-club-73704e823a01f00d461dac41dfae852471186d34.zip
Week 252
-rw-r--r--challenge-252/cheok-yin-fung/perl/ch-1.pl20
-rw-r--r--challenge-252/cheok-yin-fung/perl/ch-2.pl23
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-252/cheok-yin-fung/perl/ch-1.pl b/challenge-252/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..d76ccfecda
--- /dev/null
+++ b/challenge-252/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,20 @@
+# The Weekly Challenge 252
+# Task 1 Special Numbers
+use v5.30.0;
+use warnings;
+use Math::Prime::Util qw/divisors/;
+
+sub sn {
+ my @ints = @_;
+ my @divisors = divisors(scalar @ints);
+ my $sum = 0;
+ for (@divisors) {
+ my $j = $_-1;
+ $sum += $ints[$j]*$ints[$j]
+ }
+ return $sum;
+}
+
+use Test::More tests=>2;
+ok sn(1,2,3,4)==21;
+ok sn(2, 7, 1, 19, 18, 3)==63;
diff --git a/challenge-252/cheok-yin-fung/perl/ch-2.pl b/challenge-252/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..03c3366077
--- /dev/null
+++ b/challenge-252/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,23 @@
+# The Weekly Challenge 252
+# Task 2 Unique Sum Zero
+# Generate all combinations sum to zero,
+# [optionally] given a lower bound and an upper bound
+use v5.30.0;
+use warnings;
+use List::Util qw/sum/;
+use Algorithm::Combinatorics qw/combinations/;
+
+sub sz {
+ my $n = $_[0];
+ die "Invalid n\n" if $n <= 0;
+ my $lower = $_[1] || -$n;
+ my $upper = $_[2] || $n;
+ my @arr = ($lower..$upper);
+ my $iter = combinations(\@arr, $n);
+ while (my $c = $iter->next) {
+ my @com = $c->@*;
+ say "@com" if 0 == sum @com;
+ }
+}
+
+sz @ARGV || (5,-5,4);