DZone Forums
Go Back   DZone Forums > Community > Databases & SQL > IBM DB2
Reload this Page Fermat's Theorem solution (plain DB2)
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Sep 2009
Exclamation Fermat's Theorem solution (plain DB2) - 10-11-2009, 12:11 AM

Fermat's Theorem X^n + Y^n = Z^n where X, Y, Z, n are integer numbers and n >= 2.

I created the simple query which give you ability to play with this therem.

Code:
with pN(n, lim) as
( select 2, 500 from sysibm.sysdummy1)
,
ArgX(x, XpN) as 
(select int(1), int(1) from sysibm.sysdummy1
union all
select X + 1, power(X + 1, N)
from ArgX, pN
where X + 1 <= lim
) 
,
ArgY(y, YpN) as 
(select * from ArgX )
,
ArgZ(z, ZpN) as 
(select int(1), int(1) from sysibm.sysdummy1
union all
select Z + 1, power(Z + 1, N)
from ArgZ, pN
where Z + 1 <= lim * power(2, 1. / n) + 1
) 
select X, Y, Z, 
strip(digits(X), L, '0') || '^' || strip(digits(N), L, '0') || ' + ' ||
strip(digits(Y), L, '0') || '^' || strip(digits(N), L, '0') || ' = ' ||
strip(digits(Z), L, '0') || '^' || strip(digits(N), L, '0') "Fermat Solution"

from ArgX, ArgY, ArgZ, pN 

where 
     ZpN = XpN + YpN
and X < Y
and Y < Z
and Z < X + Y 

order by Z, X, Y
With this query you can easily get the first 500 (actually 386) arguments which are the solution for Fermat Great theorem for N = 2.

You can change N in pN table and you'll not find any argument which will
satisfy the Fermat's equation (if N >=3).

Lenny Khiger, ADSPA&VP
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Sep 2009
Lightbulb Generalized Fermat's Theorem - 10-14-2009, 10:46 PM

Generalized Fermat's Theorem:

abs(((X^n + Y^n) / Z^n) - 1.0) < eps,
where 0 < X < Y < Z


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

Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prime numbers (plain DB2) lennykhiger IBM DB2 3 10-10-2009 12:24 AM
iDao -data access object in plain Java interface shaketbaby@yahoo.com Java 0 08-31-2009 11:36 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com