Pell series – Python Program to Generate first N numbers of Pell Series

Pell series: Given the number N, the task is to print the first n numbers of the pell series in Python.

Pell Series:

The Pell Series is a series in which the next number equals the sum of twice the previous number and the number preceding the previous number.

It begins with 1 and 2.

Pell Numbers: 1,2,5,12,29,70,169,…

Consider the following number sequence and try to match it to the definition of the Pell Series:

1 + 2 x 2 = 5

2 + 5* 2 = 12

5 + 12 x 2 = 29

12 + 29*2 = 70

29 + 70*2 = 169

and so on…

Now that you have a clear idea of what the Pell Series are, let’s move on to how we shall write the Python code to generate the same.

Examples:

Example1:

Given number = 12

Input:

The first 12 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860

Output:

Example2:

Input:

Given number = 21

Output:

The first 21 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109
 15994428 38613965

Program to Generate first N numbers of Pell Series in Python

Below are the ways to generate the first N number of the pell series in Python.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable temp1 and initialize it to 1.
  • Take another variable temp2 and initialize it to 2.
  • Take the third variable temp3 and initialize it to 0.
  • Now, print the first two numbers in the series, and we’ll print the other generated numbers in the order they were generated.
  • Loop till number -2 using For loop.
  • Add the value of temp1 and twice the temp2 value and initialize the temp3 with this value.
  • Swap the previous number for the previous number’s predecessor (temp1 =temp2).
  • Swap the generated and previously generated numbers (temp2 = temp3).
  • Print the value of temp3.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
numb = 21
# Take a variable temp1 and initialize it to 1.
temp1 = 1
# Take another variable temp2 and initialize it to 2.
temp2 = 2
# Take the third variable temp3 and initialize it to 0.
temp3 = 0
# Now, print the first two numbers in the series,
# and we'll print the other generated numbers in the order they were generated.
print('The first', numb, 'numbers of the pell series are :')
print(temp1, temp2, end=' ')
# Loop till number -2 using For loop.
for i in range(numb-2):
    # Add the value of temp1 and twice the temp2 value
    # and initialize the temp3 with this value.
    temp3 = temp1+2*temp2
    # Swap the previous number for the previous number's predecessor (temp1 =temp2).
    temp1 = temp2
    # Swap the generated and previously generated numbers (temp2 = temp3)
    temp2 = temp3
    # Print the value of temp3.
    print(temp3, end=' ')

Output:

The first 21 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109
 15994428 38613965

Method #2: Using For Loop (User Input)

Approach:

  • Give the number N as user input using int(input()) and store it in a variable.
  • Take a variable temp1 and initialize it to 1.
  • Take another variable temp2 and initialize it to 2.
  • Take the third variable temp3 and initialize it to 0.
  • Now, print the first two numbers in the series, and we’ll print the other generated numbers in the order they were generated.
  • Loop till number -2 using For loop.
  • Add the value of temp1 and twice the temp2 value and initialize the temp3 with this value.
  • Swap the previous number for the previous number’s predecessor (temp1 =temp2).
  • Swap the generated and previously generated numbers (temp2 = temp3).
  • Print the value of temp3.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using int(input()) and store it in a variable.
numb = int(input('Enter some random number = '))
# Take a variable temp1 and initialize it to 1.
temp1 = 1
# Take another variable temp2 and initialize it to 2.
temp2 = 2
# Take the third variable temp3 and initialize it to 0.
temp3 = 0
# Now, print the first two numbers in the series,
# and we'll print the other generated numbers in the order they were generated.
print('The first', numb, 'numbers of the pell series are :')
print(temp1, temp2, end=' ')
# Loop till number -2 using For loop.
for i in range(numb-2):
    # Add the value of temp1 and twice the temp2 value
    # and initialize the temp3 with this value.
    temp3 = temp1+2*temp2
    # Swap the previous number for the previous number's predecessor (temp1 =temp2).
    temp1 = temp2
    # Swap the generated and previously generated numbers (temp2 = temp3)
    temp2 = temp3
    # Print the value of temp3.
    print(temp3, end=' ')

Output:

Enter some random number = 12
The first 12 numbers of the pell series are :
1 2 5 12 29 70 169 408 985 2378 5741 13860

Related Programs: