Tuesday, November 19, 2024
Google search engine

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++




// 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;
}


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.


PHP




<?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
?>


Javascript




<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
Auxiliary Space: O(n) because the function call stack grows linearly with the input n. Each function call adds a new frame to the call stack, which contains local variables and the return address.

A better solution is to use dynamic programming.  

C++




// 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;
}


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 code
print(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.


PHP




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


JavaScript




<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)
Auxiliary Space: O(n) where n is the input number. This is because dp array has been created of size n+1.

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 <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;
}


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.


PHP




<?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
?>


Javascript




<script>
// 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)
{
    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 neveropen and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments