Question: How to Generate Fibonacci Series for the First 1000 Values?
The reason, behind this question, is very simple, if you search online you will find the most of the answers only find the first 70 or maximum 80 values of the Fibonacci series. The reason behind is very simple, once you reach to 70th or 100th steps of the Fibonacci series the value is so big that often many programming languages produce the error.
Answer: In this blog post 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.
If you are going to an interview, you can for sure write a cursor where you add values but writing script which is the most efficient will bring you additional brownie points.
Let me know if you prefer any other way to write this script. Please post your solution in the comments section. I will publish it with due credit.
Reference: Pinal Dave (https://blog.sqlauthority.com)