Post date: Jun 26, 2014 1:5:37 AM
"Which term, counting from the start, the first 1000 digit element of the Fibonacci sequence?"
This is the same as my 1000th Fibonacci problem, except that it filters by length of the Fibonacci, not its position in the series. I will use the same ShiftStringRight and AddBigNumbers function.
-- See linked page
CREATE FUNCTION dbo.ShiftStringRight
...
GO
-- See linked page
CREATE FUNCTION dbo.AddBigNumbers
...
GO
-- Calculate the Fibonacci
DECLARE @nm2 varchar(max) = 0, @nm1 varchar(max) = 1, @n varchar(max), @c int = 1
WHILE 1 = 1
BEGIN
SET @n = dbo.AddBigNumbers(@nm2, @nm1)
SET @c = @c + 1
if LEN(@n) >= 1000 BREAK
SET @nm2 = @nm1
SET @nm1 = @n
END -- While
SELECT @c
GO
DROP FUNCTION dbo.AddBigNumbers
DROP FUNCTION dbo.ShiftStringRight