Python Program for Leonardo Number Series

In the previous article, we have discussed Python Program to Find the Rotation Count in Rotated Sorted List
Leonardo Number:

The Leonardo numbers are a series of numbers determined by recurrence:

L(n) =  1       if n=0

       =   1       if n=1

       =   L(n-1)+L(n-2)+1   if n>1

The first 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 in the following way:

L(n) = 2F(n+1) – 1 if n>=1

Given a number, the task is to find the Leonardo number sequence to the given number.

Examples:

Example1:

Input:

Given Number = 10

Output:

The Leonardo Number Sequence till the given number 10  = 
1
1
3
5
9
15
25
41
67
109

Example2:

Input:

Given Number = 15

Output:

The Leonardo Sequence till the given number 15 = 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219

Program for Leonardo Number Series in Python

Below are the ways to find the Leonardo Number Series using the recursive approach in Python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as a parameter to the Leonardo recursive function.
  • The base condition is defined as a value that is less than or equal to 1.
  • If the base condition is true, then return 1.
  • Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called recursively with the parameter as the number minus 2 and add 1 to it.
  • Use a for loop to return the Leonardo sequence and return the result and print the result.
  • The Exit of the program.

Below is the implementation :

# function which finds the Leonardo sequence recursively
def LeonardoRecursion(numb):
  # The base condition is defined as a value that is less than or equal to 1.
    if(numb <= 1):
        # If the base condition is true, then return 1.
        return 1
    else:
      # Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called
      # recursively with the parameter as the number minus 2 and add 1 to it.
        return(LeonardoRecursion(numb-1) + LeonardoRecursion(numb-2)+1)


# The user must give the number as static input and store it in a variable.
numb = 10
print("The Leonardo Number Sequence till the given number", numb, ' = ')
# Looping from 1 to given number using for loop
for n in range(numb):
  # passing the iterter value as argument to the recursive function LeonardoRecursion
    print(LeonardoRecursion(n))

Output:

The Leonardo Number Sequence till the given number 10  = 
1
1
3
5
9
15
25
41
67
109

Method #2: Using Recursion (User Input)

Approach:

  • The user must give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as a parameter to the Leonardo recursive function.
  • The base condition is defined as a value that is less than or equal to 1.
  • If the base condition is true, then return 1.
  • Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called recursively with the parameter as the number minus 2 and add 1 to it.
  • Use a for loop to return the Leonardo sequence and return the result and print the result.
  • The exit of the program.

Below is the implementation:

# function which finds the Leonardo sequence recursively
def LeonardoRecursion(numb):
  # The base condition is defined as a value that is less than or equal to 1.
  # If the base condition is true, then return 1.
    if(numb <= 1):
        return 1
    else:
      # Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called
      # recursively with the parameter as the number minus 2 and add 1 to it.
        return(LeonardoRecursion(numb-1) + LeonardoRecursion(numb-2)+1)


# The user must give the number as user input using the
# int(input()) function and store it in a variable.
numb = int(input('Enter some random number = '))
print("The Leonardo Sequence till the given number", numb, ' = ')
# Looping from 1 to given number using for loop
for n in range(numb):
  # passing the iterter value as argument to the recursive function LeonardoRecursion
    print(LeonardoRecursion(n))

Output:

Enter some random number = 15
The Leonardo Sequence till the given number 15 = 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.