aboutsummaryrefslogtreecommitdiff
path: root/challenge-125
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-125')
-rw-r--r--challenge-125/cheok-yin-fung/perl/ch-1.pl10
1 files changed, 8 insertions, 2 deletions
diff --git a/challenge-125/cheok-yin-fung/perl/ch-1.pl b/challenge-125/cheok-yin-fung/perl/ch-1.pl
index 10fabf76ef..8d0c3a650a 100644
--- a/challenge-125/cheok-yin-fung/perl/ch-1.pl
+++ b/challenge-125/cheok-yin-fung/perl/ch-1.pl
@@ -7,7 +7,7 @@ use warnings;
use v5.10.0;
use experimental 'signatures';
use List::Util qw/max/;
-use Test::More tests => 7;
+use Test::More tests => 8;
my $num = $ARGV[0] || 5;
my @arr = pyth($num)->@*;
@@ -37,7 +37,12 @@ sub pyth ($n) {
}
}
- for my $b0 ($n+1..$n*$n) {
+
+ # c^2 - b_max^2 >= (b_max+1)^2 - b_max^2 = 2*b_max + 1
+ # 2*b_max + 1 <= n^2
+ # b_max <= (n^2-1)/2
+
+ for my $b0 ($n+1..int ($n*$n-1)/2) {
my $is_sq = $b0*$b0 + $n*$n;
if (sqrt($is_sq) == int sqrt($is_sq)) {
push @ans, [$n , $b0 ,sqrt($is_sq)];
@@ -55,3 +60,4 @@ ok scalar @{pyth(4)} == 1, "Number 4";
ok scalar @{pyth(5)} == 2, "Number 5";
ok scalar @{pyth(8)} == 2, "Number 8";
ok scalar @{pyth(13)} == 2, "Number 13";
+ok scalar @{pyth(5740)} == 44, "Number 5740" # oeis.org/A046081