Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 02, 2012 02:15 PM by DarrellNorton
Jul 02, 2012 01:59 PM|LINK
Which one is best approach?
Yes Stored Procedure call itself but
"Maximum stored procedure, function, trigger, or view nesting level should not be exceeded (limit 32)".
-------------------------------First Approach---------Through Function-------
create function FibnocciSeries(@number1 int, @number2 int, @count int)
returns @table table (i int)
declare @number3 int
set @number3 = @number2 + @number1
set @count = @count-1
if @count <> 0
insert into @table
Jul 02, 2012 02:15 PM|LINK
The best approach to recursion is to not do it in the first place. Recursion on a sufficiently large fibonacci number will consume too much memory, or CPU, etc. until the process crashes.
The better approach is to iterate over the fib series like an array, starting at zero. For large fib numbers you will also run into data type issues (the value is too large for an int or bigint to hold for example).
This blog post shows a pretty good approach that handles most of the common fibonacci pitfalls.