aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-11-15 06:17:56 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-11-15 06:17:56 +0800
commita3c164970c4e43cc71b5fe8bd1c4a3fb02a35709 (patch)
tree3e795aba3991162232feb77eead924afe2ec14d8
parented0f1f1c40bc9491497b091456ad8f1c35c47805 (diff)
downloadperlweeklychallenge-club-a3c164970c4e43cc71b5fe8bd1c4a3fb02a35709.tar.gz
perlweeklychallenge-club-a3c164970c4e43cc71b5fe8bd1c4a3fb02a35709.tar.bz2
perlweeklychallenge-club-a3c164970c4e43cc71b5fe8bd1c4a3fb02a35709.zip
commenting
-rw-r--r--challenge-138/cheok-yin-fung/perl/ch-2.pl13
1 files changed, 9 insertions, 4 deletions
diff --git a/challenge-138/cheok-yin-fung/perl/ch-2.pl b/challenge-138/cheok-yin-fung/perl/ch-2.pl
index 3cf1916ec2..5c4d5c160d 100644
--- a/challenge-138/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-138/cheok-yin-fung/perl/ch-2.pl
@@ -22,12 +22,17 @@ sub split_number {
my $len = length $N;
my $upper = length $rt;
- my %wlen;
-
+ my %wlen; # hash to record each unordered partition
my $i = Integer::Partition->new($len);
while (my $a = $i->next) {
- next if any { $_ > $upper } @$a;
+ next if any { $_ > $upper } @$a;
+ # Explanation for above line:
+ # It is an optimization.
+ # For example, sqrt(9663676416) = 98304
+ # so we can expect partitions with number > 99999,
+ # i.e. length number > 5,
+ # cannot fulfill the requirement.
my $j = permutations($a);
while (my $b = $j->next) {
if (!defined($wlen{join ",", @$b})) {
@@ -41,7 +46,7 @@ sub split_number {
push @config, $temp;
}
if ( (sum @config) == $rt) {
- say "sqrt($N) = " ,
+ say "sqrt($N) = " , #print the formula
"$rt = ",
join " + ", map { $_ == 0 ? "(0)" : $_ } @config;
return 1;