#!/usr/bin/perl -w
use strict;
use v5.10;
use threads;
use threads::shared;
use Math::BigInt;
use Math::BigFloat qw();
# 1 3, 4, 5
# 2 20, 21, 29
# 3 119, 120, 169
# 4 696, 697, 985
# 5 4_059, 4_060, 5_741
# 6 23_660, 23_661, 33_461
# 7 137_903, 137_904, 195_025
# 8 803_760, 803_761, 1_136_689
# 9 4_684_659, 4_684_660, 6_625_109
#10 27_304_196, 27_304_197, 38_613_965
#11 159_140_519, 159_140_520, 225_058_681
#12 927_538_920, 927_538_921, 1_311_738_121
#13 5_406_093_003, 5_406_093_004, 7_645_370_045
#14 31_509_019_100, 31_509_019_101, 44_560_482_149
#15 183_648_021_599, 183_648_021_600, 259_717_522_849
#16 1_070_379_110_496, 1_070_379_110_497, 1_513_744_654_945
$| = 1;
my $precision = -1;
Math::BigFloat->precision($precision);
my $last_a = 3;
my $last_b = 4;
my $last_c = 5;
my $this_a = 20;
my $this_b = 21;
my $this_c = 29;
sub min {
my ($a, $b) = @_;
$a < $b ? $a : $b;
}
sub max {
my ($a, $b) = @_;
$a > $b ? $a : $b;
}
for (;;) {
my $ratio_a = Math::BigFloat->new($this_a)->bdiv($last_a);
my $ratio_b = Math::BigFloat->new($this_b)->bdiv($last_b);
my $ratio_c = Math::BigFloat->new($this_c)->bdiv($last_c);
my $ratio_min = min($ratio_a, min($ratio_b, $ratio_c));
my $ratio_max = max($ratio_a, max($ratio_b, $ratio_c));
my $start = Math::BigInt->new(
Math::BigFloat->new($this_a)->bmul($ratio_min)->bint()
);
my $end = Math::BigFloat->new($this_b)->bmul($ratio_max)->bint() + 1;
my $found = 0;
SEARCH: for (my $next_a = $start; $next_a < $end; $next_a++) {
my $next_b = $next_a + 1;
my $h2 = Math::BigInt->new($next_a) * $next_a
+ Math::BigInt->new($next_b) * $next_b;
my $next_c = Math::BigInt->new((Math::BigFloat->new($h2)->bsqrt() + .5)->bint());
if ($h2 == $next_c * $next_c) {
$last_a = $this_a;
$last_b = $this_b;
$last_c = $this_c;
$this_a = $next_a;
$this_b = $next_b;
$this_c = $next_c;
say "found triple: $this_a, $this_b, $this_c";
$found = 1;
last SEARCH;
}
}
#exit if $this_a == 5_406_093_003;
unless ($found) {
Math::BigFloat->precision(--$precision);
say "precision is now $precision";
}
}