Fibonacci numbers using a function.

  • Using DECIMALs with Binet's formula you can get the first 92 in the series

    WITH Constants(one,half,root5,phi,phi2,phi4,phi8,phi16,phi32,phi64) AS (

    SELECT CAST(1.0 AS DECIMAL(38,20)),

    CAST(0.5 AS DECIMAL(38,20)),

    CAST(2.2360679774997896964091736687313 AS DECIMAL(38,20)),

    CAST(1.6180339887498948482045868343656 AS DECIMAL(38,20)),

    CAST(2.6180339887498948482045868343656 AS DECIMAL(38,20)),

    CAST(6.8541019662496845446137605030969 AS DECIMAL(38,20)),

    CAST(46.978713763747791812296323521678 AS DECIMAL(38,20)),

    CAST(2206.9995468961462151779272055189 AS DECIMAL(38,20)),

    CAST(4870846.9999997946968976853425802 AS DECIMAL(38,20)),

    CAST(23725150497406.999999999999957851 AS DECIMAL(38,20))

    )

    SELECT n.Number,

    CAST(CAST((CASE WHEN n.Number & 64 = 0 THEN c.one ELSE c.phi64 END *

    CASE WHEN n.Number & 32 = 0 THEN c.one ELSE c.phi32 END *

    CASE WHEN n.Number & 16 = 0 THEN c.one ELSE c.phi16 END *

    CASE WHEN n.Number & 8 = 0 THEN c.one ELSE c.phi8 END *

    CASE WHEN n.Number & 4 = 0 THEN c.one ELSE c.phi4 END *

    CASE WHEN n.Number & 2 = 0 THEN c.one ELSE c.phi2 END *

    CASE WHEN n.Number & 1 = 0 THEN c.one ELSE c.phi END) / c.root5 + c.half AS BIGINT) AS VARCHAR(20)) AS FibNumber

    FROM master.dbo.spt_values n

    CROSS JOIN Constants c

    WHERE n.type='P'

    AND n.Number BETWEEN 1 AND 92;

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • Jeff Moden (2/16/2013)


    If we're going to do this, let's knock the problem right out of the park.

    ...

    We also come to the understanding that we can't directly calculate more than the 70th Fib number using this method because the FLOAT data-type returned by the SQRT function induces rounding errors after the precision of the return reaches just 15 digits.

    ...

    Agreed!

    Out of the park 1) Blazing speed, small maximum - Jeff did that!

    Out of the park 2) Hardware resource limited maximum

    Who's got a CLR arbitrary precision library[/url] for SQL Server?

    ETA: A .NET arbitrary precision library, though in alpha stage.

    And an integer only .NET arbitrary precision library

    Maybe even the .NET 4.0+ native System.Numerics.BigInteger

  • Who's got a CLR arbitrary precision library for SQL Server?

    BWAAAA-HAAAA!!! SQL Server is a very small park for these types of things. That brings me back to what I said earlier... I believe that if you need Fibonacce numbers larger than 70 or even the max in SQL Server, then you're probably using the wrong tool. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (2/25/2013)


    Who's got a CLR arbitrary precision library for SQL Server?

    BWAAAA-HAAAA!!! SQL Server is a very small park for these types of things. That brings me back to what I said earlier... I believe that if you need Fibonacce numbers larger than 70 or even the max in SQL Server, then you're probably using the wrong tool. 😉

    If you need any Fibonacci numbers, SQL is the wrong tool. It's the wrong tool for 1, it's the wrong tool for 10, it's the wrong tool for 100. It's always the wrong tool.

    That's not the point - the point* is whether or not we can do it, and do it as efficiently as possible, as you already did for your constrained set :).

    I just want to remove the arbitrary limit! Regrettably, I've never so much as tried any CLR.

    *With a secondary point of poking fun at homework questions in a fun and constructive manner.

Viewing 4 posts - 16 through 18 (of 18 total)

You must be logged in to reply to this topic. Login to reply