-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path066 Diophantine equation.pl
69 lines (48 loc) · 1.15 KB
/
066 Diophantine equation.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 11 February 2019
# https://github.com/trizen
# https://projecteuler.net/problem=66
# Runtime: 0.151s
use 5.010;
use strict;
use warnings;
use Math::GMPz;
use ntheory qw(sqrtint is_square);
use experimental qw(signatures);
use constant {
ONE => Math::GMPz->new(1),
ZERO => Math::GMPz->new(0),
};
sub solve_pell ($n, $w = 1) {
return () if is_square($n);
my $x = sqrtint($n);
my $y = $x;
my $z = 1;
my $r = 2 * $x;
my ($e1, $e2) = (ONE, ZERO);
my ($f1, $f2) = (ZERO, ONE);
for (1 .. $n) {
$y = $r * $z - $y;
$z = ($n - $y * $y) / $z;
$r = int(($x + $y) / $z);
my $A = $e2 + $x * $f2;
my $B = $f2;
if ($z == 1 and $A**2 - $n * $B**2 == 1) {
return ($A, $B);
}
($e1, $e2) = ($e2, $r * $e2 + $e1);
($f1, $f2) = ($f2, $r * $f2 + $f1);
}
return ();
}
my %max = (x => 0, d => -1);
foreach my $i (2 .. 1000) {
is_square($i) && next;
my $x = solve_pell($i);
if ($x > $max{x}) {
$max{x} = $x;
$max{d} = $i;
}
}
say $max{d};