Leonardo Number

The Leonardo numbers are a sequence of numbers given by the recurrence:
L(n) = \left \{   \begin{tabular}{ccc}   1 &   & if n = 0\\   1 &  &  if n = 1\\   L(n-1)+L(n-2)+1 & & if n $>$ 1\\   \end{tabular} \right \}

The first few Leonardo Numbers are 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, ···

The Leonardo numbers are related to the Fibonacci numbers by below relation:

L(n)=2F(n+1)-1, n\geq 0

Given a number n, find n-th Leonardo number.
Examples:

Input : n = 0
Output : 1

Input : n = 3
Output : 5

A simple solution is to recursively compute values.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive program to find n-th
// leonardo number.
#include <iostream>
using namespace std;
  
int leonardo(int n)
{
    if (n == 0 || n == 1)
        return 1;
    return leonardo(n - 1) + leonardo(n - 2) + 1;
}
  
int main()
{
    cout << leonardo(3);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive program to find n-th
// leonardo number.
import java.io.*;
  
class GFG {
    static int leonardo(int n)
    {
        if (n == 0 || n == 1)
            return 1;
        return (leonardo(n - 1) + leonardo(n - 2) + 1);
    }
  
    public static void main(String args[])
    {
        System.out.println(leonardo(3));
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A simple recursive program to find n-th
# leonardo number.
  
def leonardo(n) :
    if (n == 0 or n == 1) :
        return 1
    return (leonardo(n - 1) + leonardo(n - 2) + 1);
      
      
# Driver code    
print(leonardo(3))
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive program to
// find n-th leonardo number.
using System;
  
class GFG {
  
    static int leonardo(int n)
    {
        if (n == 0 || n == 1)
            return 1;
  
        return (leonardo(n - 1) + leonardo(n - 2) + 1);
    }
  
    public static void Main()
    {
        Console.WriteLine(leonardo(3));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A simple recursive PHP 
// program to find n-th
// leonardo number.
  
// function returns the 
// nth leonardo number 
function leonardo($n)
{
    if ($n == 0 || $n == 1)
        return 1;
    return leonardo($n - 1) + 
           leonardo($n - 2) + 1;
}
  
// Driver Code
echo leonardo(3);
  
// This code is contributed by ajit
?>

chevron_right



Output :

5

Time Complexity : Exponential

A better solution is to use dynamic programming.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive program to find n-th
// leonardo number.
#include <iostream>
using namespace std;
  
int leonardo(int n)
{
    int dp[n + 1];
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= n; i++)
        dp[i] = dp[i - 1] + dp[i - 2] + 1;
    return dp[n];
}
  
int main()
{
    cout << leonardo(3);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive program to find n-th
// leonardo number.
import java.io.*;
  
class GFG {
  
    static int leonardo(int n)
    {
        int dp[] = new int[n + 1];
        dp[0] = dp[1] = 1;
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + dp[i - 2] + 1;
        return dp[n];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(leonardo(3));
    }
}
  
/*This code is contributed by vt_m.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A simple recursive program 
# to find n-th leonardo number.
  
def leonardo(n):
    dp = [];
    dp.append(1);
    dp.append(1);
    for i in range(2, n + 1):
        dp.append(dp[i - 1] + 
                  dp[i - 2] + 1);
    return dp[n];
  
# Driver code
print(leonardo(3));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive program to
// find n-th leonardo number.
using System;
  
class GFG {
  
    static int leonardo(int n)
    {
        int[] dp = new int[n + 1];
        dp[0] = dp[1] = 1;
  
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + dp[i - 2] + 1;
        return dp[n];
    }
  
    public static void Main()
    {
        Console.WriteLine(leonardo(3));
    }
}
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A simple recursive program to 
// find n-th leonardo number.
  
function leonardo( $n)
{
    $dp[0] = $dp[1] = 1;
    for ($i = 2; $i <= $n; $i++)
        $dp[$i] = $dp[$i - 1] + 
                  $dp[$i - 2] + 1;
    return $dp[$n];
}
  
    echo leonardo(3);
  
// This code is contributed by ajit.
?>

chevron_right



Output :

5

Time Complexity : O(n)

The best solution is to use relation with Fibonacci Numbers. We can find n-th Fibonacci number in O(Log n) time [See method 4 of this]

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A O(Log n) program to find n-th Leonardo
// number.
#include <iostream>
using namespace std;
  
/* Helper function that multiplies 2 matrices 
   F and M of size 2*2, and puts the 
   multiplication result back to F[][] */
void multiply(int F[2][2], int M[2][2])
{
    int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
    int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
    int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
    int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
}
  
void power(int F[2][2], int n)
{
    int i;
    int M[2][2] = { { 1, 1 }, { 1, 0 } };
  
    // n - 1 times multiply the matrix
    // to {{1, 0}, {0, 1}}
    for (i = 2; i <= n; i++)
        multiply(F, M);
}
  
int fib(int n)
{
    int F[2][2] = { { 1, 1 }, { 1, 0 } };
    if (n == 0)
        return 0;
    power(F, n - 1);
    return F[0][0];
}
  
int leonardo(int n)
{
    if (n == 0 || n == 1)
        return 1;
    return 2 * fib(n + 1) - 1;
}
  
int main()
{
    cout << leonardo(3);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A O(Log n) program to find n-th Leonardo
// number.
  
class GFG {
  
    /* Helper function that multiplies 2 matrices 
    F and M of size 2*2, and puts the 
    multiplication result back to F[][] */
    static void multiply(int F[][], int M[][])
    {
        int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
        int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
        int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
        int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
        F[0][0] = x;
        F[0][1] = y;
        F[1][0] = z;
        F[1][1] = w;
    }
  
    static void power(int F[][], int n)
    {
        int i;
        int M[][] = { { 1, 1 }, { 1, 0 } };
  
        // n - 1 times multiply the matrix
        // to {{1, 0}, {0, 1}}
        for (i = 2; i <= n; i++)
            multiply(F, M);
    }
  
    static int fib(int n)
    {
        int F[][] = { { 1, 1 }, { 1, 0 } };
        if (n == 0)
            return 0;
        power(F, n - 1);
        return F[0][0];
    }
  
    static int leonardo(int n)
    {
        if (n == 0 || n == 1)
            return 1;
        return 2 * fib(n + 1) - 1;
    }
  
    public static void main(String args[])
    {
        System.out.println(leonardo(3));
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A O(Log n) program to find n-th Leonardo
# number.
  
# Helper function that multiplies 2 matrices 
# F and M of size 2 * 2, and puts the 
# multiplication result back to F[][] 
def multiply(F, M ) :
    x = F[0][0] * M[0][0] + F[0][1] * M[1][0]
    y = F[0][0] * M[0][1] + F[0][1] * M[1][1]
    z = F[1][0] * M[0][0] + F[1][1] * M[1][0]
    w = F[1][0] * M[0][1] + F[1][1] * M[1][1]
    F[0][0] = x
    F[0][1] = y
    F[1][0] = z
    F[1][1] = w
   
def power(F, n) :
    M = [[ 1, 1 ], [ 1, 0 ] ]
   
    # n - 1 times multiply the matrix
    # to {{1, 0}, {0, 1}}
    for i in range(2, n + 1) :
        multiply(F, M)
          
  
def fib(n) :
    F = [ [ 1, 1 ], [ 1, 0 ] ]
    if (n == 0) :
        return 0
    power(F, n - 1)
    return F[0][0]
   
def leonardo(n) :
    if (n == 0 or n == 1) :
        return 1
    return (2 * fib(n + 1) - 1)
      
# main method    
print(leonardo(3))
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A O(Log n) program to find
// n-th Leonardo number.
using System;
  
class GFG {
  
    /* Helper function that multiplies 2 matrices 
       F and M of size 2*2, and puts the 
       multiplication result back to F[][] */
    static void multiply(int[, ] F, int[, ] M)
    {
        int x = F[0, 0] * M[0, 0] + F[0, 1] * M[1, 0];
        int y = F[0, 0] * M[0, 1] + F[0, 1] * M[1, 1];
        int z = F[1, 0] * M[0, 0] + F[1, 1] * M[1, 0];
        int w = F[1, 0] * M[0, 1] + F[1, 1] * M[1, 1];
        F[0, 0] = x;
        F[0, 1] = y;
        F[1, 0] = z;
        F[1, 1] = w;
    }
  
    static void power(int[, ] F, int n)
    {
        int i;
        int[, ] M = { { 1, 1 }, { 1, 0 } };
  
        // n - 1 times multiply the matrix
        // to {{1, 0}, {0, 1}}
        for (i = 2; i <= n; i++)
            multiply(F, M);
    }
  
    static int fib(int n)
    {
        int[, ] F = { { 1, 1 }, { 1, 0 } };
        if (n == 0)
            return 0;
        power(F, n - 1);
        return F[0, 0];
    }
  
    static int leonardo(int n)
    {
        if (n == 0 || n == 1)
            return 1;
        return 2 * fib(n + 1) - 1;
    }
  
    // Driver Code
    public static void Main()
    {
        Console.WriteLine(leonardo(3));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A O(Log n) program to find n-th 
// Leonardo number.
  
/* Helper function that multiplies 2 matrices 
F and M of size 2*2, and puts the 
multiplication result back to $F][] */
function multiply(&$F, $M)
{
    $x = $F[0][0] * $M[0][0] + 
         $F[0][1] * $M[1][0];
    $y = $F[0][0] * $M[0][1] + 
         $F[0][1] * $M[1][1];
    $z = $F[1][0] * $M[0][0] + 
         $F[1][1] * $M[1][0];
    $w = $F[1][0] * $M[0][1] + 
         $F[1][1] * $M[1][1];
    $F[0][0] = $x;
    $F[0][1] = $y;
    $F[1][0] = $z;
    $F[1][1] = $w;
}
  
function power(&$F, $n)
{
    $M = array(array(1, 1), array(1, 0));
  
    // n - 1 times multiply the matrix
    // to {{1, 0}, {0, 1}}
    for ($i = 2; $i <= $n; $i++)
        multiply($F, $M);
}
  
function fib($n)
{
    $F = array(array(1, 1), array(1, 0));
    if ($n == 0)
        return 0;
    power($F, $n - 1);
    return $F[0][0];
}
  
function leonardo($n)
{
    if ($n == 0 || $n == 1)
        return 1;
    return 2 * fib($n + 1) - 1;
}
  
// Driver Code
echo leonardo(3);
  
//This code is contributed by mits
?>

chevron_right



Output :

5

Time Complexity : O(Log n)

This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : jit_t, Mithun Kumar