Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspxI have listened to the critique in the comments and removed the advanced version where you can calculate virtually any Fibonacci number. Contact me if you have the need for it again. I won't bother non-interested people with the algorithm here. Instead,enCommunityServer 2.1 SP2 (Build: 61129.1)re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#17884Sun, 18 Oct 2009 14:16:30 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:17884Paul Nielsen<p>very cool. well done. </p>
<p>-Pn</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#17954Mon, 19 Oct 2009 17:53:16 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:17954John Meriweather<p>What's the point of calculating the Fibonacci series in T-SQL? And why would anyone use t-SQL for that calculation?</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18003Tue, 20 Oct 2009 15:00:44 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18003Jason<p>@Peter: This is a great example of a bad post. </p>
<p>First of all, you only provide a heap of code with no explanation as to why it is interesting (thanks Peso) or how you might use it.</p>
<p>Second, your code essentially implements the addition operator for strings instead of just using the built in "+", which is just stupid. </p>
<p>@Paul: This post is neither cool nor well done.</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18012Tue, 20 Oct 2009 17:25:16 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18012Linchi Shea<p>I don't think John was questioning the significance of the Fibonacci series, but rather why one would do it with T-SQL. For classroom exercises and seminar demos, it's fine. It's also fine if one just wants to kill some time. But I'd agree that people should probably be cautioned not to actually end up writing production T-SQL code to do Fibonacci searies. Well, maybe there is a real case where writing it in T-SQL is justified. But it is certianly difficult to come up with one.</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18022Tue, 20 Oct 2009 18:54:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18022Peso<p>New content posted.</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18068Wed, 21 Oct 2009 13:51:18 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18068Jason<p>Hi Peter,</p>
<p>I like the revisions you made to this article. Much more interesting. </p>
<p>As an alternative to BIGINT, you could use DECIMAL(38,0) and calculate up to the 184th number in the sequence.</p>
<p>Thanks, Jason</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18221Sun, 25 Oct 2009 13:49:50 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18221Joe Celko<p>Wouldn't it be faster to use the constant phi and the closed form?</p>
<p>Fibonacci(@n INTEGER)</p>
<p>AS </p>
<p>RETURN</p>
<p> EOUND (((POWER (1.6190339887, @n)- POWER (1.0 - 1.6190339887, @n))/ </p>
<p>SQRT (5.0)), 0);</p>
<p>untested.</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18228Sun, 25 Oct 2009 16:28:46 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18228Peso<p>It would be great if it worked! However, the golden ration is the ration for which division of Fibonacci number converges to, when sufficiently large.</p>
<p>However, the formula you displayed above gives wrong answer very early, and after step 12, the error is larger and larger</p>
<p>n Peso Celko</p>
<p>-- ---- -----</p>
<p>1 0 0</p>
<p>2 1 1</p>
<p>3 1 1</p>
<p>4 2 2</p>
<p>5 3 3</p>
<p>6 5 5</p>
<p>7 8 8</p>
<p>8 13 13</p>
<p>9 21 21</p>
<p>10 34 34</p>
<p>11 55 55</p>
<p>12 89 90</p>
<p>13 144 145</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18240Mon, 26 Oct 2009 06:27:40 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18240RBArryYoung<p>This one is accurate up to n=62:</p>
<p> FLOOR(POWER(1.61803398874989, n)/SQRT(5) +.5)</p>
<p>:-)</p>re: Simple Fibonacci calculationhttp://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx#18241Mon, 26 Oct 2009 06:52:24 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:18241Peso<p>Much better. </p>