Difference between revisions of "Lucas number"

From Lazarus wiki
(Created page with "= Lucas number = The Lucas Sequence is the series of numbers: 2, 1, 3, 4, 7, 11, 18, 29, 47, ... == Recursive way == <syntaxhighlight> function LucasNumber( n : intege...")
 
(Recursive way: indentation)
Line 13: Line 13:
 
function LucasNumber( n : integer ): integer;
 
function LucasNumber( n : integer ): integer;
 
begin
 
begin
   if n > 1 then result := LucasNumber( n - 1 ) + LucasNumber( n - 2 )
+
   if n > 1 then  
    else
+
    result := LucasNumber( n - 1 ) + LucasNumber( n - 2 )
      if n = 0 then result := 2
+
  else
        else result := 1;
+
    if n = 0 then  
 +
      result := 2
 +
    else  
 +
      result := 1;
 
end;  
 
end;  
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
== Making use of [[Fibonacci number]]s  ==
 
== Making use of [[Fibonacci number]]s  ==

Revision as of 11:33, 16 November 2014

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;