Calculating Fibonacci Series using SQL Server 2008

In this article I describe how to calculate Fibonacci series using SQL Query.
  • 5209

Introduction

Fibonacci series is a series of number in which first two numbers are 0 and 1, and each number is a sum of preceding two numbers. Fibonacci series is denoted by F(n) where n=0 or 1. Fibonacci numbers are:

F(0)= 0,1,1,2,3,5,8,13,21,34,55,89,144.......
F(1)= 1,1,2,3,5,8,13,21,34,55,89,144.......

Calculating Fibonacci series

There are various methods to calculate Fibonacci series, here I am describing simplest way to  calculate Fibonacci series with the help of SQL Server.

Example:

Type following code in editor window to calculate Fibonacci series.

declare @X int
declare @Y int
declare @FIB int

set @X = 1
set @Y = 1
set @FIB = 0

print @X
print @Y

while @FIB < 100
BEGIN
set @FIB = @X + @Y
print @FIB
set @X = @Y
set @Y = @FIB
END

Now select whole code and press F5 to execute selected code.

Output:

Fibonacciseries-in-sql.jpg


Categories

More Articles

© 2013 dotNetheaven. All rights reserved.