Generate Fibonacci Series for the First 1000 Values

What is the fibonacci sequence

The Fibonacci sequence is a series of numbers where the next number is the sum of previous two numbers.

  • Fibonacci sequence formula : fn = f(n-1)+f(n-2)
  • The first two numbers of the Fibonacci series is 0 followed by 1.
  • 0 1 1 2 3 5 8 13 21 34 …..
                 
fibonacci series

Fibonacci Series

           
 
It is very easy to generate Fibonacci series up to the max size of the datatype, if it crosses the max size of a particular datatype then many programming languages produces the error.

 

So we will try to find an answer to Fibonacci in till 1000th value.

Here is the script which will generate first 1001 value of the Fibonacci series.

SELECT 0 AS Fibonacci
UNION ALL
SELECT
FLOOR(POWER(( 1 + SQRT(5)) / 2.0, number) / SQRT(5) + 0.5)
FROM master..spt_values
WHERE TYPE = ‘p’ AND number < 1000

You can always write Fibonacci series by writing cursor where you add previous values to the next value, however, that solution is very expensive and takes pretty long time to execute. Using the script which I have demonstrated, you will get the results pretty quickly.

Please watch my previous post Logical Questions in that post I have written Factorial, Fibonacci, Palindrome and etc using C# language.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *