If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Fermat's Theorem solution (plain DB2)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 10-12-09, 11:12
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by DB2Plus
This is for performance and removing duplicates ?

Kara S.
Yes, it is !

Lenny
Reply With Quote
  #17 (permalink)  
Old 10-14-09, 17:59
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Generalized Fermat's Theorem -- experiment

Generalized Fermat's Theorem:
Code:
with pN(n, lim, eps) as
( select 3, 300, double(1e-6) from sysibm.sysdummy1)
,
ArgX(x, XpN) as 
(select int(1), double(1) from sysibm.sysdummy1
union all
select X + 1, power(double(X + 1), N)
from ArgX, pN
where X + 1 <= lim
) 
,
ArgY(y, YpN) as 
(select * from ArgX )
,
ArgZ(z, ZpN) as 
(select int(1), double(1) from sysibm.sysdummy1
union all
select Z + 1, power(double(Z + 1), N)
from ArgZ, pN
where Z + 1 <= lim * power(2, 1. / n) + 1
) 
select X argX, Y argY, Z argZ , N "Power", (ZpN - (XpN + YpN)) "Absolute Difference" , 
double(ZpN) / double(XpN + YpN) - 1.0 "Relative Difference" 

from ArgX, ArgY, ArgZ, pN 
where ZpN between (XpN + YpN) * ( 1. - eps) and (XpN + YpN) * ( 1. + eps) 
and X < Y
and Y < Z
and X < Z
and Z <= X + Y 
order by abs(ZpN - (XpN + YpN)), X, Y
We can make some experiment:

For eps = 1e-6 we have 17 solutions in this interval....
But for eps = 1e-7 we have only one solution:

Code:
select power(135, 3) + power(235, 3) - power(249, 3) 
from sysibm.sysdummy1
We can see 135^3 + 235^3 = 248^3 + 1 (not bad !).

For eps = 1e-8 we have no solutions in this interval, even for Generalized
Fermat's Theorem.

Maybe that's why Fermat's Theorem doesn't have solutions when n >=3.

Lenny Khiger, ADSPA&VP
Reply With Quote
  #18 (permalink)  
Old 10-15-09, 11:20
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Post more interesting results

Quote:
242^3 + 720^3 = 729^3 - 1
244^3 + 729^3 = 738^3 + 1
334^3 + 438^3 = 495^3 - 1
372^3 + 426^3 = 505^3 - 1
426^3 + 486^3 = 577^3 - 1
566^3 + 833^3 = 904^3 - 1
791^3 + 812^3 = 1010^3 - 1
etc...

All you can get using GFT with lim = 1000, n = 3, eps = 1E-8

Code:
with pN(n, lim, eps) as
( select 3, 1000, double(1e-8) from sysibm.sysdummy1)....
Lenny

Last edited by Lenny77; 10-19-09 at 15:07.
Reply With Quote
  #19 (permalink)  
Old 10-19-09, 15:23
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down N = 4

For N = 4 (X^4 + Y^4 = Z^4) is not such nice:

Code:
with pN(n, lim, eps) as
( select 4, 700, double(1e-7) from sysibm.sysdummy1)
,
ArgX(x, XpN) as 
(select int(1), double(1) from sysibm.sysdummy1
union all
select X + 1, power(double(X + 1), N)
from ArgX, pN
where X + 1 <= lim
) 
,
ArgY(y, YpN) as 
(select * from ArgX )
,
ArgZ(z, ZpN) as 
(select int(1), double(1) from sysibm.sysdummy1
union all
select Z + 1, power(double(Z + 1), N)
from ArgZ, pN
where Z + 1 <= lim * power(2, 1. / n) + 1
) 
select X "..X.." , Y "..Y..", Z "..Z.." , N "Power", 
(ZpN - (XpN + YpN)) "Absolute Difference" , 
double(ZpN) / double(XpN + YpN) - 1.0 "Relative Difference" 

from ArgX , ArgY, ArgZ, pN  
where ZpN between (XpN + YpN) * ( 1. - eps) and (XpN + YpN) * ( 1. + eps) 
and X < Y
and Y < Z
and X < Z
and Z <= X + Y 
order by abs(ZpN - (XpN + YpN)), X, Y
Result:

Quote:
..X.. ..Y.. ..Z.. Power Absolute Difference Relative Difference
167 192 215 4 -1.92000000000000E+002 -8.98560555129269E-008
242 471 479 4 1.10400000000000E+003 2.09713808541068E-008
334 384 430 4 -3.07200000000000E+003 -8.98560555129269E-008
216 647 649 4 5.18400000000000E+003 2.92204040963640E-008
179 635 636 4 -1.22900000000000E+004 -7.51144320215724E-008
501 576 645 4 -1.55520000000000E+004 -8.98560555129269E-008
452 602 645 4 1.69930000000000E+004 9.81818568668302E-008
496 627 681 4 -1.77760000000000E+004 -8.26505138634692E-008
For eps = 1e-9 we don't have any solutions for this interval.

Lenny
Reply With Quote
  #20 (permalink)  
Old 10-20-09, 14:26
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down N = 5

Code:
with pN(n, lim, eps) as
( select 5, 1000, double(1e-8) from sysibm.sysdummy1)
,
ArgX(x, XpN) as 
(select int(1), double(1) from sysibm.sysdummy1
union all
select X + 1, power(double(X + 1), N)
from ArgX, pN
where X + 1 <= lim
) 
,
ArgY(y, YpN) as 
(select * from ArgX )
,
ArgZ(z, ZpN) as 
(select int(1), double(1) from sysibm.sysdummy1
union all
select Z + 1, power(double(Z + 1), N)
from ArgZ, pN
where Z + 1 <= lim * power(2, 1. / n) + 1
) 
select X "..X.." , Y "..Y..", Z "..Z.." , N "Power", 
(ZpN - (XpN + YpN)) "Absolute Difference" , 
double(ZpN) / double(XpN + YpN) - 1.0 "Relative Difference" 

from ArgX , ArgY, ArgZ, pN  
where ZpN between (XpN + YpN) * ( 1. - eps) and (XpN + YpN) * ( 1. + eps) 
and X < Y
and Y < Z
and X < Z
and Z <= X + Y 
order by abs(ZpN - (XpN + YpN)), X, Y
Result:

Quote:
..X.. ..Y.. ..Z.. Power Absolute Difference Relative Difference
494 954 961 5 2.43155300000000E+006 2.96665314536426E-009
So far, so bad !

Lenny
Reply With Quote
  #21 (permalink)  
Old 10-26-09, 13:06
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb modification to Fermat's solution

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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On