Leonardo Number - GeeksforGeeks

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:  
Input : n = 0
Output : 1

Input : n = 3
Output : 5
A simple solution is to recursively compute values.  
C++
Java
Python3
C#
PHP
Javascript
// 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;}
// 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.*/
# 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.
// 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.
<?php// A simple recursive PHP// program to find n-th// leonardo number. // function returns the// nth leonardo numberfunction leonardo($n){    if ($n == 0 || $n == 1)        return 1;    return leonardo($n - 1) +           leonardo($n - 2) + 1;} // Driver Codeecho leonardo(3); // This code is contributed by ajit?>
<script>    // Javascript program to find n-th// leonardo number.         function leonardo(n)    {        let dp = [];        dp[0] = dp[1] = 1;        for (let i = 2; i <= n; i++)            dp[i] = dp[i - 1] + dp[i - 2] + 1;        return dp[n];    }         // Driver code     document.write(leonardo(3));     </script>
Output : 
5
Time Complexity : Exponential
A better solution is to use dynamic programming.  
C++
Java
Python3
C#
PHP
JavaScript
// 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;}
// 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.*/
# 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
// 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.
<?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.?>
<script>// A simple recursive program to find n-th// leonardo number.    function leonardo(n)    {        var dp = Array.from({length: n+1}, (_, i) => 0);        dp[0] = dp[1] = 1;        for (var i = 2; i <= n; i++)            dp[i] = dp[i - 1] + dp[i - 2] + 1;        return dp[n];    }     // Driver code   document.write(leonardo(3)); // This code contributed by Princi Singh</script>
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++
Java
Python3
C#
PHP
Javascript
// 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;}
// 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.*/
# 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.
// 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.
<?php// A O(Log n) program to find n-th// Leonardo number. /* Helper function that multiplies 2 matricesF and M of size 2*2, and puts themultiplication 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 Codeecho leonardo(3); //This code is contributed by mits?>
<script>// A O(Log n) program to find n-th Leonardo// number.   /* Helper function that multiplies 2 matricesF and M of size 2*2, and puts themultiplication result back to F */function multiply(F , M){    var x = F[0][0] * M[0][0] + F[0][1] * M[1][0];    var y = F[0][0] * M[0][1] + F[0][1] * M[1][1];    var z = F[1][0] * M[0][0] + F[1][1] * M[1][0];    var 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){    var i;    var 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);} function fib(n){    var F = [ [ 1, 1 ], [ 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;}  document.write(leonardo(3)); // This code is contributed by Amit Katiyar</script>
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. 
jit_t
Mithun Kumar
sanjoy_62
princi singh
amit143katiyar
Fibonacci
series
Mathematical
Mathematical
series
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Operators in C / C++