Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Generate Fibonacci series - No loop, no recursion

Sep 1 2009 2:21PM by Madhivanan   

Generating the Fibonnaci series generally involves in writing the code which has while loop or a 
recursive function

Peso posted the code using the Common table expression

Here are my methods that involve no loop or no recursion

1 Use Quirky update technique

declare @fib table(fib_id bigint,fib_val bigint)

declare @current_val bigint, @previous_val bigint,@new_val bigint

--Set how many rows you want to generate the series

insert into @fib(fib_id,fib_val ) 

select top 30 row_number() over(order by name),1 from sysobjects 

select @current_val =0, @previous_val=-1,@new_val =1 
 

update @fib 
set 
        fib_val =@current_val,
        @current_val =@new_val +@previous_val,
        @previous_val=@new_val, 
        @new_val=@current_val 

select * from @fib 
2 Use Generalised formula
--Method 1

select
        ceiling((power(1.61803398874989,number )-POWER(-1/1.61803398874989,number))/SQRT(5)) as fib_number
from 
        master..spt_values 
where 
        TYPE='p' and number between 0 and 100 

-- Method 2

select 
        round(((power((1+SQRT(5))/2.0,number )-POWER(-1/(1+SQRT(5))/2.0,number))/SQRT(5)),0) as fib_number
from 
        master..spt_values 
where 
        TYPE='p' and number between 0 and 100 

-- Method 3 (shortest method)

select 
        floor(power((1+SQRT(5))/2.0,number )/sqrt(5)+0.5) as fib_number
from 
        master..spt_values 
where 
        TYPE='p' and number between 0 and 100

Tags: t-sql, sql_server, fibonacci_series,


Madhivanan
3 · 40% · 12968
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Hi Madhivanan,

    I populated temp table using methods you suggested and did some comparisons(see code below.)

    drop table #t select number, ceiling((power(1.61803398874989,number )-POWER(-1/1.61803398874989,number))/SQRT(5)) as method1, round(((power((1+SQRT(5))/2.0,number )-POWER(-1/(1+SQRT(5))/2.0,number))/SQRT(5)),0) as method2, floor(power((1+SQRT(5))/2.0,number )/sqrt(5)+0.5) as method3, cast(null as float) as QuirkyUpdate into #t from master..spt_values where TYPE='p' and number between 0 and 100

    --Implicit conversion to float happens to method1,method2 and method3 as we can see from

    select typename(xtype),name from syscolumns where id=objectid('tempdb..#t')

    declare @fib table(fibid float,fibval float) declare @currentval float, @previousval float,@newval float insert into @fib(fibid,fibval ) select cast(number as float),1 from master..sptvalues where TYPE='p' and number between 1 and 100

    select @currentval =0, @previousval=-1,@newval =1 update @fib set fibval =@currentval, @currentval =@newval +@previousval, @previousval=@newval, @newval=@currentval update #t set QuirkyUpdate=fibval from @fib where fibid=number+1

    select * from #t

    select min(number) from #t where method1<>QuirkyUpdate --63 select min(number) from #t where method2<>QuirkyUpdate --71 select min(number) from #t where method2!=method3 --77

    It shows that method 1 is limited to the first 62 numbers and then gives incorrect results. QuirkyUpdate is better but up to number 72. Methods 2 and 3 appear more reliable, but because of the nature of float datatype it's hard to say if they are correct for higher numbers.

    But calculating high values of fib numbers is more mental exersize than practical. So we can use either of your methods(I prefer method3 as the shortest) if numbers of interest are under known limits.

    Leonid

    commented on Apr 19 2010 3:27PM
    Leonid Koyfman
    50 · 4% · 1183
  • Leonid Koyfman,

    Thanks for the feedback

    commented on Apr 20 2010 7:11AM
    Madhivanan
    3 · 40% · 12968

Your Comment


Sign Up or Login to post a comment.

"Generate Fibonacci series - No loop, no recursion" rated 5 out of 5 by 1 readers
Generate Fibonacci series - No loop, no recursion , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]