Pell Series

Generate Pell series (0, 1, 2, 5, 12, ...).

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

What's going on

Pell series: P(n) = 2*P(n-1) + P(n-2).

Key Concepts:

Start with 0, 1
Each next = 2*previous + previous-1
Special recurrence relation