Python Program to Print nth Iteration of Lucas Sequence

In the previous article, we have discussed Python Program to Find Sum of Geometric Progression Series

Definition of Lucas sequence:

We’ve all heard of the Fibonacci sequence. It is a sequence in which each term is the sum of the two preceding terms. The Lucas sequence is the same as the previous one, but with different starting values. A Fibonacci sequence starts with 0 and 1, whereas a Lucas sequence in python starts with 2 and 1. The other terms in the Lucas sequence are 3, 4, 7, 11, Nth Lucas Number In Python, Lucas Series In Python, Lucas Series In Java and so on.

Given a number ‘n’ and the task is to print the given nth iteration of Lucas Sequence.

Examples:

Example1:

Input:

n = 6

Output:

The above Given nth iteration of Lucas Sequence =  18

Example 2:

Input:

n = 10

Output:

The above Given nth iteration of Lucas Sequence =  123

Program to Print nth Iteration of Lucas Sequence

Below are the ways to get the given nth Iteration of the Lucas Sequence.

Method #1: Using For Loop  (Static Input)

Approach:

  • Give the First term =2 (since the first term in Lucas Sequence is 2 which is a constant) as static input and store it in a variable.
  • Give the Second term =1 (since the second term in Lucas Sequence is 1 which is a constant) as static input and store it in another variable.
  • Give the number as static input and store it in another variable.
  • Loop from ‘1’ to the above given n+1 value (since doesn’t include the last term) range using For loop.
  • Inside the loop, get the third term which is the sum of the first and the second term, and store it in a variable.
  • Assign the value of the second term to the first term.
  • Assign the value of the third term to the second term and come out of For Loop.
  • Print the Value of the above given nth iteration of Lucas Sequence(i.e. first term).
  • The Exit of the program.

Below is the implementation:

# Give the First term =2 (since the first term in Lucas Sequence is 2 which is a constant)
# as static input and store it in a variable.
fst_trm = 2
# Give the Second term =1 (since the second term in Lucas Sequence is 1 which is a constant)
# as static input and store it in another variable.
secnd_trm = 1
# Give the number as static input and store it in another variable.
gvn_n_vlue = 6
# Loop from '1' to the above given n+1 value (since doesn't include last term) range
# using For loop.
for i in range(1, gvn_n_vlue+1):
 # Inside the loop , get the third term which is the sum of first and the second term
    # and store it in a variable.
    third_trm = fst_trm+secnd_trm
 # Assign the value of second term to the first term.
    fst_trm = secnd_trm
  # Assign the value of the third term to the second term and come out of For Loop.
    secnd_trm = third_trm
# Print the Value of above given nth iteration of Lucas Sequence(i.e. first term).
print("The above Given nth iteration of Lucas Sequence = ", fst_trm)

Output:

The above Given nth iteration of Lucas Sequence =  18

Method #2: Using For Loop  (User Input)

Approach:

  • Give the First term =2 (since the first term in Lucas Sequence is 2 which is a constant) as static input and store it in a variable.
  • Give the Second term =1 (since the second term in Lucas Sequence is 1 which is a constant) as static input and store it in another variable.
  • Give the number as User input and store it in another variable.
  • Loop from ‘1’ to the above given n+1 value (since doesn’t include the last term) range using For loop.
  • Inside the loop, get the third term which is the sum of the first and the second term, and store it in a variable.
  • Assign the value of the second term to the first term.
  • Assign the value of the third term to the second term and come out of For Loop.
  • Print the Value of the above given nth iteration of Lucas Sequence(i.e. first term).
  • The Exit of the program.

Below is the implementation:

# Give the First term =2 (since the first term in Lucas Sequence is 2 which is a constant)
# as static input and store it in a variable.
fst_trm = 2
# Give the Second term =1 (since the second term in Lucas Sequence is 1 which is a constant)
# as static input and store it in another variable.
secnd_trm = 1
# Give the number as User input and store it in another variable.
gvn_n_vlue = int(input("Enter Some Random number = "))
# Loop from '1' to the above given n+1 value (since doesn't include last term) range
# using For loop.
for i in range(1, gvn_n_vlue+1):
 # Inside the loop , get the third term which is the sum of first and the second term
    # and store it in a variable.
    third_trm = fst_trm+secnd_trm
 # Assign the value of second term to the first term.
    fst_trm = secnd_trm
  # Assign the value of the third term to the second term and come out of For Loop.
    secnd_trm = third_trm
# Print the Value of above given nth iteration of Lucas Sequence(i.e. first term).
print("The above Given nth iteration of Lucas Sequence = ", fst_trm)

Output:

Enter Some Random number = 10
The above Given nth iteration of Lucas Sequence = 123

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.

Read more: Python Program to Find Vertex, Focus and Directrix of Parabola

Solve these:

  1. Print The Nth Lucas Number In Python?
  2. Given A Number N, Print The Nth Lucas Numbers python. Numbering Starts From 0?

Related Posts On: