Lucas Series

Generate Lucas series (2, 1, 3, 4, 7, ...).

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

Output

Enter number of terms: 8
2 1 3 4 7 11 18 29

What's going on

Lucas series: starts with 2, 1.

Key Concepts:

Start with 2, 1
Each next = sum of previous two
Similar to Fibonacci