Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Pell Series

Generate Pell numbers with pair updates. Students multiply the previous term by two before adding the earlier term and handle short sequence counts explicitly.

AdvancedPhase 6 - Category 4: Mathematical CombinationsExample 10 of 10
pell-series.py
1# Take n
2n = int(input("Enter number of terms: "))
3
4# Generate Pell series
5if n <= 0:
6 print("Invalid input")
7elif n == 1:
8 print("0")
9elif n == 2:
10 print("0 1")
11else:
12 a, b = 0, 1
13 print(a, b, end=" ")
14 for i in range(2, n):
15 c = 2 * b + a
16 print(c, end=" ")
17 a, b = b, c
18 print()

Output

Enter number of terms: 8
0 1 2 5 12 29 70 169

In this one

  • Apply the Pell recurrence
  • Preserve old pair values
  • Handle initial terms

How it works

  1. 1

    Handle counts zero through two with dedicated branches.

  2. 2

    Compute c = 2 * b + a.

  3. 3

    Print c before updating a and b.

Edge cases

What is the next term after 0, 1, 2?

What is the next term after 0, 1, 2?

Five.

What if n = 2?

What if n = 2?

Only zero and one print.

What's going on

The starting terms are zero and one. Each new c is 2 * b + a, where b is the latest term and a the one before it.

Simultaneous assignment shifts the pair only after c is calculated. For five terms, the output is 0 1 2 5 12. This is not ordinary Fibonacci addition. Nonpositive counts are rejected before any sequence values print.

What is the next term after 0, 1, 2? Five.

What if n = 2? Only zero and one print.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which recurrence is used?

Pick an answer to check it.

Question 2

What is the next term after 0, 1, 2?

Pick an answer to check it.

Questions people ask

What is the next term after 0, 1, 2?

Five.

What if n = 2?

Only zero and one print.

Which recurrence is used?

2 * b + a.