Lucas number

From Lazarus wiki
Revision as of 12:33, 16 November 2014 by BigChimp (talk | contribs) (Recursive way: indentation)

Lucas number

The Lucas Sequence is the series of numbers:

2, 1, 3, 4, 7, 11, 18, 29, 47, ...


Recursive way

function LucasNumber( n : integer ): integer;
begin
  if n > 1 then 
    result := LucasNumber( n - 1 ) + LucasNumber( n - 2 )
  else
    if n = 0 then 
      result := 2
    else 
      result := 1;
end;

Making use of Fibonacci numbers

function LucasNumber2( n : integer ): integer;
begin
  result := FibonacciNumber( n + 1 ) + FibonacciNumber( n - 1 );
end;