Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963

    Arrow 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
    With this query we 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.


    Lenny K (Citigroup)
    Last edited by sathyaram_s; 07-23-09 at 11:40.

  2. #2
    Join Date
    Jul 2009
    Posts
    150

    Lightbulb

    If you'll add
    and X.A + Y.A > Z.A
    you'll make the query faster.

    Thanks, Kara.

  3. #3
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963
    Better performance with Z.A < X.A + Y.A

  4. #4
    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
    Lenny (Citigroup)

  5. #5
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by Lenny77


    Lenny (Citigroup)

    Good job man! It's so nice to see the bail-out money hard at work. Can you create a perpetual motion machine in SQL?
    ---
    "It does not work" is not a valid problem statement.

  6. #6
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963
    Quote Originally Posted by n_i
    Good job man! It's so nice to see the bail-out money hard at work. Can you create a perpetual motion machine in SQL?
    I can do everything. What about you ?

  7. #7
    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(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
    Lenny
    Last edited by Lenny77; 07-22-09 at 16:53.

  8. #8
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Lenny77
    What is the next step?
    ...
    and X < Y
    and Y < Z
    and X < Z
    dude.

    learn to simplify.

    that is all.

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  9. #9
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963
    Quote Originally Posted by r937
    dude.

    learn to simplify.

    that is all.

    If you'll explain to me how it's affecting the performance I'll increase you salary on 10% !

  10. #10
    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

  11. #11
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963

    Question

    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.
    How many cows, and how many chickens has a farmer ?

    You have to find solution, using the code shown above.
    Last edited by Lenny77; 07-27-09 at 15:54.

  12. #12
    Join Date
    Jul 2009
    Posts
    150
    Quote Originally Posted by Lenny77
    We can find solution of the problem like a problem
    with cows and chickens on a farm:




    How many cows, and how many chickens has a farmer ?

    You have to find solution, using the code shown above.
    We can remove the calculation of Power, I think....

  13. #13
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963
    Quote Originally Posted by DB2Plus
    We can remove the calculation of Power, I think....
    See solution (about farm) below:

    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_Heads = 92
    No_of_Leags = 294.

    Result:

    COWS CHICKENS
    55.............37

    Lenny K
    Last edited by Lenny77; 07-31-09 at 16:49.

  14. #14
    Join Date
    Jul 2009
    Location
    NY
    Posts
    963

    Thumbs up

    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_nbr
    Lenny K
    Last edited by Lenny77; 07-31-09 at 16:50.

  15. #15
    Join Date
    Jul 2009
    Posts
    150
    .....
    and X < Y
    and Y < Z
    and X < Z
    and Z < X + Y
    .....
    This is for performance and removing duplicates ?

    Kara S.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •