diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-11-15 12:27:07 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-11-15 12:27:07 +0800 |
| commit | d26dc349a8d0e9a3a8825952bb2d97d1b27e31ce (patch) | |
| tree | 11e2646d34cec567b77e224535f36360bbea4edc | |
| parent | 4f3b607b4fabd7693f8ee8d9c2291cdddd614b25 (diff) | |
| download | perlweeklychallenge-club-d26dc349a8d0e9a3a8825952bb2d97d1b27e31ce.tar.gz perlweeklychallenge-club-d26dc349a8d0e9a3a8825952bb2d97d1b27e31ce.tar.bz2 perlweeklychallenge-club-d26dc349a8d0e9a3a8825952bb2d97d1b27e31ce.zip | |
fix case N=1 and fix optimization
| -rw-r--r-- | challenge-138/cheok-yin-fung/perl/ch-2.pl | 16 |
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 |
