diff options
| -rw-r--r-- | challenge-252/cheok-yin-fung/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-252/cheok-yin-fung/perl/ch-2.pl | 23 |
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); |
