I have made some modification to original Fermat's solution:
1. Remove all solutions where GCD(X, Y) > 1
for this I add to query:
Code:
Divisors(div) as
(select int(2) from sysibm.sysdummy1
union all
(select div + 1 from Divisors, pn
where div <= sqrt(start + lim) * power(2, 1. / n) + 1))
.....
.....
.....
and not exists
(select 1 from Divisors F
where mod(X.A, F.div) = 0
and mod(Y.A, F.div) = 0
and X.A > F.div
and Y.A > F.div )
)
2. Get solutions not in interval [0, lim], but in interval [start, start + lim]
Quote:
2020 2121 2929 2020^2 + 2121^2 = 2929^2
2059 2100 2941 2059^2 + 2100^2 = 2941^2
2060 2163 2987 2060^2 + 2163^2 = 2987^2
2040 2201 3001 2040^2 + 2201^2 = 3001^2
2117 2244 3085 2117^2 + 2244^2 = 3085^2
2140 2247 3103 2140^2 + 2247^2 = 3103^2
2184 2263 3145 2184^2 + 2263^2 = 3145^2
2180 2289 3161 2180^2 + 2289^2 = 3161^2
2120 2409 3209 2120^2 + 2409^2 = 3209^2
2175 2392 3233 2175^2 + 2392^2 = 3233^2
2260 2373 3277 2260^2 + 2373^2 = 3277^2
2325 2332 3293 2325^2 + 2332^2 = 3293^2
2387 2484 3445 2387^2 + 2484^2 = 3445^2
|
Query:
Code:
with pN(n, lim, start) as
( select 2, 500, 2009 from sysibm.sysdummy1)
,
Divisors(div) as
(select int(2) from sysibm.sysdummy1
union all
(select div + 1 from Divisors, pn
where div <= sqrt(start + lim) * power(2, 1. / n) + 1)
)
,
Arguments(A, ApN, start, n, lim) as
(select decimal(start, 31, 0), decimal(power(start, N), 31, 0),
decimal(start, 31, 0), n, lim
from pN
union all
select A + 1, power(A + 1, N), start, n, lim
from Arguments
where A + 1 <= (start + lim) * power(2, 1. / n) + 1
)
select X.A x, Y.A y, Z.A z,
replace(
varchar(X.A) || '^' || varchar(x.N) || ' + ' ||
varchar(Y.A) || '^' || varchar(y.N) || ' = ' ||
varchar(Z.A) || '^' || varchar(z.N), '.', '' ) "Fermat Solution"
from Arguments X, Arguments Y, Arguments Z
where
X.ApN + Y.ApN = Z.ApN
and Y.A > X.A
and Z.A > Y.A
and X.A + Y.A > Z.A
and X.A <= x.start + x.lim
and Y.A <= y.start + y.lim
and not exists
(select 1 from Divisors F
where mod(X.A, F.div) = 0
and mod(Y.A, F.div) = 0
and X.A > F.div
and Y.A > F.div )
order by Z.A, X.A, Y.A
Lenny Khiger, ADSPA&VP