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

Lucas Series

Generate Lucas numbers with a two-value update. Students compare Lucas starting values with Fibonacci and trace the same addition rule on different seeds.

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

In this one

  • Choose a different seed pair
  • Keep a two-term recurrence
  • Validate the term count

How it works

  1. 1

    Handle nonpositive counts and short outputs.

  2. 2

    Start the longer case from two and one.

  3. 3

    Print each sum and advance a, b together.

Edge cases

What does n = 1 print?

What does n = 1 print?

Two.

Is the addition rule different from Fibonacci?

Is the addition rule different from Fibonacci?

No. The starting pair differs.

What's going on

The Lucas sequence here starts two, one. Subsequent terms add the previous two, just as Fibonacci does with different initial values.

Short branches print one or two terms directly. Otherwise the loop prints new sums and shifts the pair together. Five terms are 2 1 3 4 7. The entered n is a term count, and nonpositive values get an invalid-input message.

What does n = 1 print? Two.

Is the addition rule different from Fibonacci? No. The starting pair differs.

Quiz

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

Question 1

What follows 2, 1, 3?

Pick an answer to check it.

Question 2

What does n = 1 print?

Pick an answer to check it.

Questions people ask

What does n = 1 print?

Two.

Is the addition rule different from Fibonacci?

No. The starting pair differs.

What follows 2, 1, 3?

4.