diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-11-15 12:48:40 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-11-15 12:48:40 +0800 |
| commit | 0acead8055a6b88bda956b3855f24f86d87ede2a (patch) | |
| tree | 45d23d16ba99a846266f9d116372ad6e1703996f | |
| parent | d26dc349a8d0e9a3a8825952bb2d97d1b27e31ce (diff) | |
| download | perlweeklychallenge-club-0acead8055a6b88bda956b3855f24f86d87ede2a.tar.gz perlweeklychallenge-club-0acead8055a6b88bda956b3855f24f86d87ede2a.tar.bz2 perlweeklychallenge-club-0acead8055a6b88bda956b3855f24f86d87ede2a.zip | |
length ./ > does not do the optimization!
| -rw-r--r-- | challenge-138/cheok-yin-fung/perl/ch-2.pl | 47 |
1 files changed, 43 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 3efbf50fdc..807a17d70a 100644 --- a/challenge-138/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-138/cheok-yin-fung/perl/ch-2.pl @@ -7,7 +7,7 @@ use warnings; use List::Util qw/ any sum /; use Integer::Partition; use Algorithm::Combinatorics qw / permutations /; -use Test::More tests => 3; +use Test::More tests => 6; my $NUM = $ARGV[0] || 1; @@ -26,13 +26,17 @@ sub split_number { return 0 if $N == 1; #after reading Abigail's code my $i = Integer::Partition->new($len); while (my $a = $i->next) { - next if any { (length $_) > $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. + + # NOT:::::: next if any { length $_ > $upper } @$a; + # (2nd public version) + my $j = permutations($a); while (my $b = $j->next) { if (!defined($wlen{join ",", @$b})) { @@ -62,12 +66,16 @@ sub split_number { ok split_number(81) == 1, "Example 1"; ok split_number(9801) == 1, "Example 2"; ok split_number(36) == 0, "Example 3"; +ok split_number(999*999) == 1, "test case 1 with 10^n-1"; +ok split_number(9999*9999) == 1, "test case 2 with 10^n-1"; +ok split_number(17073424) == 1, "final test" -#for fun -# grep { 1 == split_number($_*$_)} 1..100; =pod +#for fun + grep { 1 == split_number($_*$_)} 100..5000; + Output: sqrt(81) = 9 = 8 + 1 sqrt(100) = 10 = 10 + 0 @@ -78,4 +86,35 @@ sqrt(6724) = 82 = 6 + 72 + 4 sqrt(8281) = 91 = 82 + 8 + 1 sqrt(9801) = 99 = 98 + 01 sqrt(10000) = 100 = 100 + 00 +sqrt(55225) = 235 = 5 + 5 + 225 +sqrt(88209) = 297 = 88 + 209 +sqrt(136161) = 369 = 1 + 361 + 6 + 1 +sqrt(136900) = 370 = 1 + 369 + (0) +sqrt(143641) = 379 = 14 + 364 + 1 +sqrt(171396) = 414 = 17 + 1 + 396 +sqrt(431649) = 657 = 4 + 3 + 1 + 649 +sqrt(455625) = 675 = 45 + 5 + 625 +sqrt(494209) = 703 = 494 + 209 +sqrt(571536) = 756 = 5 + 715 + 36 +sqrt(627264) = 792 = 62 + 726 + 4 +sqrt(826281) = 909 = 826 + 2 + 81 +sqrt(842724) = 918 = 842 + 72 + 4 +sqrt(893025) = 945 = 8 + 930 + 2 + 5 +sqrt(929296) = 964 = 929 + 29 + 6 +sqrt(980100) = 990 = 980 + 10 + (0) +sqrt(982081) = 991 = 982 + 8 + 1 +sqrt(998001) = 999 = 998 + 1 +sqrt(1000000) = 1000 = 1000 + (0) +sqrt(1679616) = 1296 = 1 + 679 + 616 +sqrt(2896804) = 1702 = 2 + 896 + 804 +sqrt(3175524) = 1782 = 3 + 1755 + 24 +sqrt(4941729) = 2223 = 494 + 1729 +sqrt(7441984) = 2728 = 744 + 1984 +sqrt(11329956) = 3366 = 11 + 3299 + 56 +sqrt(13293316) = 3646 = 1 + 329 + 3316 +sqrt(13557124) = 3682 = 1 + 3557 + 124 +sqrt(17073424) = 4132 = 1 + 707 + 3424 +sqrt(23804641) = 4879 = 238 + (0) + 4641 +sqrt(24068836) = 4906 = 2 + 4068 + 836 +sqrt(24502500) = 4950 = 2450 + 2500 |
