Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Fibonacci numbers using a function. Expand / Collapse
Author
Message
Posted Sunday, February 17, 2013 6:08 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:55 PM
Points: 3,648, Visits: 5,321
You can also do this with a recursive CTE, which can be put into an iTVF.

See the first example, in the third article in my signature links.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1421017
Posted Monday, February 18, 2013 12:32 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:07 PM
Points: 37,075, Visits: 31,632
dwain.c (2/17/2013)
You can also do this with a recursive CTE, which can be put into an iTVF.

See the first example, in the third article in my signature links.


Yes, you could. But even Peter Larsson's super trimmed down rCTE has a "counting rCTE" as a base. It may not matter much for single use but the formula method runs in sub-millisecond times whereas the rCTE takes 38 milliseconds (on my ever so humble desktop) for elements 0 through 70. You should check out the rads, as well. Recursion just ins't the answer for things like this unless it's to build a lookup table just one time.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1421326
Posted Monday, February 18, 2013 5:31 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:55 PM
Points: 3,648, Visits: 5,321
Jeff Moden (2/18/2013)
dwain.c (2/17/2013)
You can also do this with a recursive CTE, which can be put into an iTVF.

See the first example, in the third article in my signature links.


Yes, you could. But even Peter Larsson's super trimmed down rCTE has a "counting rCTE" as a base. It may not matter much for single use but the formula method runs in sub-millisecond times whereas the rCTE takes 38 milliseconds (on my ever so humble desktop) for elements 0 through 70. You should check out the rads, as well. Recursion just ins't the answer for things like this unless it's to build a lookup table just one time.


I agree that the formula method would be faster even without running the actual test. That was a very clever approach by the way Jeff.

I was just offering an alternative that could be put into an iTVF (unlike the loop).



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1421389
Posted Tuesday, February 19, 2013 9:26 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:07 PM
Points: 37,075, Visits: 31,632
Ah. Understood. It would be interesting to find out which is actually faster, though. The mTVF with a loop or the iTVF with the recursive CTE.

--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1421720
Posted Tuesday, February 19, 2013 5:12 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:55 PM
Points: 3,648, Visits: 5,321
Jeff Moden (2/19/2013)
Ah. Understood. It would be interesting to find out which is actually faster, though. The mTVF with a loop or the iTVF with the recursive CTE.


Hehe. Normally I would take that challenge but I'm a bit swamped with other stuff going on right now. I agree that it would be interesting.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1421897
Posted Wednesday, February 20, 2013 3:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 7:45 AM
Points: 1,694, Visits: 19,552
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;



____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1422007
Posted Monday, February 25, 2013 2:35 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 11:09 AM
Points: 869, Visits: 2,399
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 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
Post #1423778
Posted Monday, February 25, 2013 5:13 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:07 PM
Points: 37,075, Visits: 31,632
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1423825
Posted Tuesday, February 26, 2013 8:42 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 11:09 AM
Points: 869, Visits: 2,399
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.
Post #1424116
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse