Talk:Lucas number
From Lazarus wiki
Jump to navigationJump to searchQ: Why not use UInt64 as return type for the function. It should be the same on all platforms.
Remark: it seems a bit strange to me to derive Lucas(n) from Fib(n), because the Fibonacci sequence is jus a special case of the Lucas numbers (with starting values 1,1). --Bart (talk) 19:15, 10 November 2018 (CET)
Here's an example of what I mean:
{
A more general form of the Lucas series, where the 2 starting values
can be set using parameters
}
function LucasGen(L1, L2, N: UInt64): UInt64;
var
i, LucMin1, LucMin2: UInt64;
begin
if (N = 0) then
Raise ERangeError.Create('Lucas function is undefined for 0.');
if (N = 1) then
Exit(L1)
else if (N = 2) then
Exit(L2)
else
begin
LucMin1 := L2;
LucMin2 := L1;
i := 2;
while (i <> N) do
begin
Inc(i);
Result := LucMin2 + LucMin1;
LucMin2 := LucMin1;
LucMin1 := Result;
end;
end;
end;
function Lucas(N: UInt64): UInt64;
begin
Result := LucasGen(2,1,N);
end;
function Fib(N: UInt64): UInt64;
begin
Result := LucasGen(1,1,N);
end;