# Leonardo Number

• Difficulty Level : Easy
• Last Updated : 28 May, 2021

The Leonardo numbers are a sequence of numbers given by the recurrence:

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:

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

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input : n = 0
Output : 1

Input : n = 3
Output : 5

A simple solution is to recursively compute values.

## C++

 // A simple recursive program to find n-th// leonardo number.#include 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;}

## Java

 // 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.*/

## Python3

 # 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.

## C#

 // 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.



## Javascript



Output :

5

Time Complexity : Exponential

A better solution is to use dynamic programming.

## C++

 // A simple recursive program to find n-th// leonardo number.#include 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;}

## Java

 // 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.*/

## Python3

 # 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 codeprint(leonardo(3)); # This code is contributed by mits

## C#

 // 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.



## JavaScript



Output :

5

Time Complexity : O(n)

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

## C++

 // A O(Log n) program to find n-th Leonardo// number.#include 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;}

## Java

 // 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.*/

## Python3

 # 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.

## C#

 // 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.



## Javascript



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