aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-15 04:35:58 +0000
committerGitHub <noreply@github.com>2021-11-15 04:35:58 +0000
commit034d103bbfe85374455b3a84b6f43cb6d38e9a3e (patch)
treece06f96595d1a5f78788ece00b5cef973d46863b
parent814dadd73434d2f7fa6db52a424d6b059913be64 (diff)
parentd26dc349a8d0e9a3a8825952bb2d97d1b27e31ce (diff)
downloadperlweeklychallenge-club-034d103bbfe85374455b3a84b6f43cb6d38e9a3e.tar.gz
perlweeklychallenge-club-034d103bbfe85374455b3a84b6f43cb6d38e9a3e.tar.bz2
perlweeklychallenge-club-034d103bbfe85374455b3a84b6f43cb6d38e9a3e.zip
Merge pull request #5222 from E7-87-83/newt
fix case N=1 and fix optimization
-rw-r--r--challenge-138/cheok-yin-fung/perl/ch-2.pl16
1 files changed, 7 insertions, 9 deletions
diff --git a/challenge-138/cheok-yin-fung/perl/ch-2.pl b/challenge-138/cheok-yin-fung/perl/ch-2.pl
index 5c4d5c160d..3efbf50fdc 100644
--- a/challenge-138/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-138/cheok-yin-fung/perl/ch-2.pl
@@ -23,15 +23,15 @@ sub split_number {
my $upper = length $rt;
my %wlen; # hash to record each unordered partition
-
+ return 0 if $N == 1; #after reading Abigail's code
my $i = Integer::Partition->new($len);
while (my $a = $i->next) {
- next if any { $_ > $upper } @$a;
+ next if any { (length $_) > $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,
+ # 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) {
@@ -64,13 +64,11 @@ ok split_number(9801) == 1, "Example 2";
ok split_number(36) == 0, "Example 3";
-=pod for fun
-for my $num (1..100) {
- split_number($num*$num);
-}
+#for fun
+# grep { 1 == split_number($_*$_)} 1..100;
+=pod
Output:
-sqrt(1) = 1 = 1
sqrt(81) = 9 = 8 + 1
sqrt(100) = 10 = 10 + 0
sqrt(1296) = 36 = 1 + 29 + 6