Solved

sql query to insert the fibonacci sequence into a table

Posted on 2013-12-31
2
478 Views
Last Modified: 2014-02-19
I need an insert statement to insert the fibonacci sequence into a table. Lets think for a moment that the table has two fields

fibid - is an integer, identity field
fibnum is where the fibonacci sequence should go.

so it will be something like this

fibid(1,2,3,4,5)
fibnum(1,2,3,5,8)

etc

lets say populate 200 records

thanks,
Vinnie
0
Comment
Question by:damixa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39749180
There are many ways to solve this problem, with different performances.....

But for a novice and easy to understand the below is the best approach

Step 1 : Create a table fibonacci with the required fields
step 2 : create a loop
Step 3 : caliculate the current fibonacci number
step 4: Store it in the table

DECLARE @T TABLE
(
fbid BIGINT IDENTITY(1,1)
,Fib BIGINT
)

DECLARE @FibonacciCurrent BIGINT
DECLARE @FibonacciPast BIGINT
DECLARE @Temp BIGINT
DECLARE @Counter BIGINT
SET @FibonacciCurrent = 1
SET @FibonacciPast = 1
WHILE (@Counter < 200)
BEGIN
   INSERT INTO @t (fib)
   SELECT @FibonacciCurrent

   SET @temp = @FibonacciCurrent  
   SET @FibonacciCurrent = @FibonacciCurrent + @FibonacciPast
   SET @FibonacciPast = @temp

   SET @Counter = @counter + 1
END

SELECT * FROM @T

Open in new window

0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39754568
check this.
;WITH Fibonacci(n, f, f1)
AS (
        -- This is the anchor part
        -- Initialize level to 1 and set the first two values as per definition
        SELECT  CAST(1 AS BIGINT),
                CAST(0 AS BIGINT),
                CAST(1 AS BIGINT)

        UNION ALL

        -- This is the recursive part
        -- Calculate the next Fibonacci value using the previous two values
        -- Shift column (place) for the sum in order to accomodate the previous
        -- value too because next iteration need them both
        SELECT  n + 1,
                f + f1,
                f
        FROM    Fibonacci
        -- Stop at iteration 93 because we than have reached maximum limit
        -- for BIGINT in Microsoft SQL Server
        WHERE   n < 93
)
-- Now the easy presentation part
SELECT  n,
        f AS Number
FROM    Fibonacci

Open in new window

http://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question