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++
// A simple recursive program to find n-th
// leonardo number.
#include <iostream>
usingnamespacestd;
intleonardo(intn)
{
if(n == 0 || n == 1)
return1;
returnleonardo(n - 1) + leonardo(n - 2) + 1;
}
intmain()
{
cout << leonardo(3);
return0;
}
Java
// A simple recursive program to find n-th
// leonardo number.
importjava.io.*;
classGFG {
staticintleonardo(intn)
{
if(n == 0|| n == 1)
return1;
return(leonardo(n - 1) + leonardo(n - 2) + 1);
}
publicstaticvoidmain(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.
defleonardo(n) :
if(n ==0orn ==1) :
return1
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.
usingSystem;
classGFG {
staticintleonardo(intn)
{
if(n == 0 || n == 1)
return1;
return(leonardo(n - 1) + leonardo(n - 2) + 1);
}
publicstaticvoidMain()
{
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
functionleonardo($n)
{
if($n== 0 || $n== 1)
return1;
returnleonardo($n- 1) +
leonardo($n- 2) + 1;
}
// Driver Code
echoleonardo(3);
// This code is contributed by ajit
?>
Javascript
<script>
// Javascript program to find n-th
// leonardo number.
functionleonardo(n)
{
let dp = [];
dp[0] = dp[1] = 1;
for(let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] + 1;
returndp[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>
usingnamespacestd;
intleonardo(intn)
{
intdp[n + 1];
dp[0] = dp[1] = 1;
for(inti = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] + 1;
returndp[n];
}
intmain()
{
cout << leonardo(3);
return0;
}
Java
// A simple recursive program to find n-th
// leonardo number.
importjava.io.*;
classGFG {
staticintleonardo(intn)
{
intdp[] = newint[n + 1];
dp[0] = dp[1] = 1;
for(inti = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] + 1;
returndp[n];
}
// Driver code
publicstaticvoidmain(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.
defleonardo(n):
dp =[];
dp.append(1);
dp.append(1);
fori inrange(2, n +1):
dp.append(dp[i -1] +
dp[i -2] +1);
returndp[n];
# Driver code
print(leonardo(3));
# This code is contributed by mits
C#
// A simple recursive program to
// find n-th leonardo number.
usingSystem;
classGFG {
staticintleonardo(intn)
{
int[] dp = newint[n + 1];
dp[0] = dp[1] = 1;
for(inti = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] + 1;
returndp[n];
}
publicstaticvoidMain()
{
Console.WriteLine(leonardo(3));
}
}
// This code is contributed by vt_m.
PHP
<?php
// A simple recursive program to
// find n-th leonardo number.
functionleonardo( $n)
{
$dp[0] = $dp[1] = 1;
for($i= 2; $i<= $n; $i++)
$dp[$i] = $dp[$i- 1] +
$dp[$i- 2] + 1;
return$dp[$n];
}
echoleonardo(3);
// This code is contributed by ajit.
?>
JavaScript
<script>
// A simple recursive program to find n-th
// leonardo number.
functionleonardo(n)
{
vardp = Array.from({length: n+1}, (_, i) => 0);
dp[0] = dp[1] = 1;
for(vari = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2] + 1;
returndp[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>
usingnamespacestd;
/* Helper function that multiplies 2 matrices
F and M of size 2*2, and puts the
multiplication result back to F[][] */
voidmultiply(intF[2][2], intM[2][2])
{
intx = F[0][0] * M[0][0] + F[0][1] * M[1][0];
inty = F[0][0] * M[0][1] + F[0][1] * M[1][1];
intz = F[1][0] * M[0][0] + F[1][1] * M[1][0];
intw = 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;
}
voidpower(intF[2][2], intn)
{
inti;
intM[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);
}
intfib(intn)
{
intF[2][2] = { { 1, 1 }, { 1, 0 } };
if(n == 0)
return0;
power(F, n - 1);
returnF[0][0];
}
intleonardo(intn)
{
if(n == 0 || n == 1)
return1;
return2 * fib(n + 1) - 1;
}
intmain()
{
cout << leonardo(3);
return0;
}
Java
// A O(Log n) program to find n-th Leonardo
// number.
classGFG {
/* Helper function that multiplies 2 matrices
F and M of size 2*2, and puts the
multiplication result back to F[][] */
staticvoidmultiply(intF[][], intM[][])
{
intx = F[0][0] * M[0][0] + F[0][1] * M[1][0];
inty = F[0][0] * M[0][1] + F[0][1] * M[1][1];
intz = F[1][0] * M[0][0] + F[1][1] * M[1][0];
intw = 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;
}
staticvoidpower(intF[][], intn)
{
inti;
intM[][] = { { 1, 1}, { 1, 0} };
// n - 1 times multiply the matrix
// to {{1, 0}, {0, 1}}
for(i = 2; i <= n; i++)
multiply(F, M);
}
staticintfib(intn)
{
intF[][] = { { 1, 1}, { 1, 0} };
if(n == 0)
return0;
power(F, n - 1);
returnF[0][0];
}
staticintleonardo(intn)
{
if(n == 0|| n == 1)
return1;
return2* fib(n + 1) - 1;
}
publicstaticvoidmain(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[][]
defmultiply(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
defpower(F, n) :
M =[[ 1, 1], [ 1, 0] ]
# n - 1 times multiply the matrix
# to {{1, 0}, {0, 1}}
fori inrange(2, n +1) :
multiply(F, M)
deffib(n) :
F =[ [ 1, 1], [ 1, 0] ]
if(n ==0) :
return0
power(F, n -1)
returnF[0][0]
defleonardo(n) :
if(n ==0orn ==1) :
return1
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.
usingSystem;
classGFG {
/* Helper function that multiplies 2 matrices
F and M of size 2*2, and puts the
multiplication result back to F[][] */
staticvoidmultiply(int[, ] F, int[, ] M)
{
intx = F[0, 0] * M[0, 0] + F[0, 1] * M[1, 0];
inty = F[0, 0] * M[0, 1] + F[0, 1] * M[1, 1];
intz = F[1, 0] * M[0, 0] + F[1, 1] * M[1, 0];
intw = 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;
}
staticvoidpower(int[, ] F, intn)
{
inti;
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);
}
staticintfib(intn)
{
int[, ] F = { { 1, 1 }, { 1, 0 } };
if(n == 0)
return0;
power(F, n - 1);
returnF[0, 0];
}
staticintleonardo(intn)
{
if(n == 0 || n == 1)
return1;
return2 * fib(n + 1) - 1;
}
// Driver Code
publicstaticvoidMain()
{
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][] */
functionmultiply(&$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;
}
functionpower(&$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);
}
functionfib($n)
{
$F= array(array(1, 1), array(1, 0));
if($n== 0)
return0;
power($F, $n- 1);
return$F[0][0];
}
functionleonardo($n)
{
if($n== 0 || $n== 1)
return1;
return2 * fib($n+ 1) - 1;
}
// Driver Code
echoleonardo(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 */
functionmultiply(F , M)
{
varx = F[0][0] * M[0][0] + F[0][1] * M[1][0];
vary = F[0][0] * M[0][1] + F[0][1] * M[1][1];
varz = F[1][0] * M[0][0] + F[1][1] * M[1][0];
varw = 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;
}
functionpower(F , n)
{
vari;
varM = [ [ 1, 1 ], [ 1, 0 ] ];
// n - 1 times multiply the matrix
// to {{1, 0], [0, 1}}
for(i = 2; i <= n; i++)
multiply(F, M);
}
functionfib(n)
{
varF = [ [ 1, 1 ], [ 1, 0 ] ];
if(n == 0)
return0;
power(F, n - 1);
returnF[0][0];
}
functionleonardo(n)
{
if(n == 0 || n == 1)
return1;
return2 * 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!