Results 1 to 15 of 21

072009, 17:34 #1Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Unanswered: Fermat's Theorem solution (plain DB2)
Code:with pN(n, lim) as ( select 2, 500 from sysibm.sysdummy1) , Arguments(A, ApN) as (select int(1), int(1) from sysibm.sysdummy1 union all select A + 1, power(A + 1, N) from Arguments, pN where A + 1 <= lim ) select X.A x, Y.A y, Z.A z, strip(digits(X.A), l, '0')  '^'  strip(digits(N), l, '0')  ' + '  strip(digits(Y.A), l, '0')  '^'  strip(digits(n), l, '0')  ' = '  strip(digits(Z.A), l, '0')  '^'  strip(digits(N), l, '0') "Fermat Solution" from Arguments X, Arguments Y, Arguments Z, pN where X.ApN + Y.ApN = Z.ApN and Y.A > X.A and Z.A > X.A and Z.A > Y.A
You can change N in pN table and you'll not find any argument which will
satisfy the Fermat's equation.
Lenny K (Citigroup)Last edited by sathyaram_s; 072309 at 10:40.

072009, 20:00 #2Registered User
 Join Date
 Jul 2009
 Posts
 150
If you'll add
and X.A + Y.A > Z.A
you'll make the query faster.
Thanks, Kara.

072109, 11:43 #3Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Better performance with Z.A < X.A + Y.A

072109, 12:19 #4Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Next step: Correction and optimization:
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

072109, 13:07 #5:)
 Join Date
 Jun 2003
 Location
 Toronto, Canada
 Posts
 5,516
Provided Answers: 1Originally Posted by Lenny77
Good job man! It's so nice to see the bailout money hard at work. Can you create a perpetual motion machine in SQL?
"It does not work" is not a valid problem statement.

072109, 17:58 #6Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Originally Posted by n_i

072209, 14:32 #7Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
What is the next step ?
Generalization Fermat's theorem and find some solution for power > 2 (n > 2) .
Equity X^n + Y^n = Z^n we can transform to
Z^n / (X^n + Y^n) = 1.
For n > 2 we'll look for solution in the interval +epsilon < 1:
abs(Z^n / (X^n + Y^n)  1) <= epsilon, or
Z^n between (X^n + Y^n) * ( 1  epsilon) and (X^n + Y^n) * ( 1 + epsilon)
So we have to use the following query instead of the basic query:
with pN(n, lim, eps) as
( select 3, 500, double(1e6) 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, YLast edited by Lenny77; 072209 at 15:53.

072209, 18:27 #8SQL Consultant
 Join Date
 Apr 2002
 Location
 Toronto, Canada
 Posts
 20,002
Originally Posted by Lenny77
learn to simplify.
that is all.

072309, 09:51 #9Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Originally Posted by r937

072309, 17:55 #10Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
In the simular way we can solve the problem of linear programming (optimization) which requested by economics.
Lenny

072709, 10:51 #11Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
We can find solution of the problem like a problem
with cows and chickens on a farm:
Some farmer has some number of cows and chickens:
The number of heads is equal to 92,
The number of legs is equal to 294.
You have to find solution, using the code shown above.
Last edited by Lenny77; 072709 at 14:54.

072909, 06:38 #12Registered User
 Join Date
 Jul 2009
 Posts
 150
Originally Posted by Lenny77

073109, 09:52 #13Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
Originally Posted by DB2Plus
with pN(lim) as
( select 500 from sysibm.sysdummy1)
,
Arguments(A) as
(select int(0)
from sysibm.sysdummy1
union all
select A + 1 from Arguments, pN
where A + 1 <= lim
)
select X.A cows , Y.A chickens
from Arguments x, Arguments y
where X.A + Y.A = 92
and 4 * X.A + 2 * Y.A = 294
No_of_Leags = 294.
Result:
COWS CHICKENS
55.............37
Lenny KLast edited by Lenny77; 073109 at 15:49.

073109, 15:43 #14Registered User
 Join Date
 Jul 2009
 Location
 NY
 Posts
 963
My friend just give me solution which looks nicer:
with pN(head_nbr,leg_nbr) as
( select 92 head_nbr, 294 leg_nbr
from sysibm.sysdummy1)
,
Arguments(A) as
(select int(0)
from sysibm.sysdummy1
union all
select A + 1 from Arguments, pN
where A + 1 <= leg_nbr
)
select X.A cows , Y.A chickens
from Arguments x, Arguments y
, pN
where
X.A + Y.A = head_nbr
and
4 * X.A + 2 * Y.A = leg_nbrLast edited by Lenny77; 073109 at 15:50.

101109, 00:19 #15Registered User
 Join Date
 Jul 2009
 Posts
 150
.....
and X < Y
and Y < Z
and X < Z
and Z < X + Y
.....
Kara S.