--ChallengeNumber20_FibNumrepeats.sql
;with fib ( FiboNumber, previousNumber, rowCnt, numRepeats )
as (
select convert(bigint,1), convert(bigint,0), 1, 0
union all
select FiboNumber, previousNumber, rowCnt,
sign(charindex('00',convert(varchar(25),FiboNumber)))+
sign(charindex('11',convert(varchar(25),FiboNumber)))+
sign(charindex('22',convert(varchar(25),FiboNumber) ))+
sign(charindex('33',convert(varchar(25),FiboNumber) ))+
sign(charindex('44',convert(varchar(25),FiboNumber) ))+
sign(charindex('55',convert(varchar(25),FiboNumber) ))+
sign(charindex('66',convert(varchar(25),FiboNumber) ))+
sign(charindex('77',convert(varchar(25),FiboNumber) ))+
sign(charindex('88',convert(varchar(25),FiboNumber) ))+
sign(charindex('99',convert(varchar(25),FiboNumber) )) as numRepeats
from
(select previousNumber+FiboNumber as FiboNumber, FiboNumber as previousNumber , rowCnt+1 as rowCnt
from fib
where rowCnt<92
) T
)
select NumRepeats, FiboNumber
from (
select FiboNumber, numRepeats,rowCnt
, row_number() over (partition by numRepeats order by rowCnt) as RowNum
from fib where numRepeats >=1) T
where RowNum <=5
Did you find something incorrect/wrong with this solution? Take a few seconds to Report It.
Did you understand how this solution work? If you find it difficult to understand, you can Request an Explanation or you can Write an explanation to help others better understand this solution.