Lucas sequence in Python
A Lucas sequence is a number sequence with a recursive relationship between its numbers where each proceeding number is the sum of its two previous numbers. In this sequence, the ratio of successive terms eventually approaches the golden ratio, where the ratio of two terms equals the ratio of their sum to the larger of the two quantities.
In mathematical form, the sequence can be represented as follows:
Relationship with the Fibonacci sequence
The Lucas sequence is very familiar to the Fibonacci series, especially regarding calculating the proceeding value. Lucas sequence has a different starting value, but its values can be formed by adding two Fibonacci sequence values that share a common adjacent value.
Let's see how the Lucas sequence is formed using the Fibonacci sequence values to understand the relation and code the sequence in Python.
Iterative approach
In this example, we will use a for loop to iterate over the index positions specified in the code and calculate the subsequent terms.
#Iterative functiondef lucasSeq(index):if index == 0:return [2]if index == 1:return [2, 1]#Initialize the Lucas sequencetempSeq = [2, 1]#Calculate subsequent termsfor i in range(2, index + 1):nextTerm = tempSeq[i - 1] + tempSeq[i - 2]tempSeq.append(nextTerm)return tempSeq#Driver codeindex = 8mySeq = lucasSeq(index)print(mySeq)
Code explanation
Line 2: Define an iterative function
lucasSeq()to create a Lucas sequence and pass the terms count as a parameter.Lines 4–7: Define the return values for
indexvalues 0 and 1.Line 10: Create a temporary list
tempSeqand save the first two values in it.Line 13–15: Create a for loop from index
2toindex + 1and calculate thenextTermfor the corresponding index by adding the value on theindex - 1andindex - 2from thetempSeq.Line 15: Add the calculated value to the
tempSeqlist usingappend().Line 16: Return the obtained
tempSeqto the driver code.Line 20: Set an
indexvalue that represents the number of Lucas sequence terms we want to output on the console.
Line 21: Call the iterative function
lucasSeq()by sending the index value as a parameter and saving the returned results in themySeqinstance.Line 22: Print the obtained Lucas sequence on the console using
print().
Recursive approach
In this example, we will use divide the problem into smaller chunks and then call the lucasSeq() function recursively until the base case is satisfied and returns the calculated sequence.
#Recursive functiondef lucasSeq(index):if index == 0:return 2elif index == 1:return 1else:return lucasSeq(index - 1) + lucasSeq(index - 2)#Driver codeindex = 8mySeq = [lucasSeq(i) for i in range(index + 1)]print(mySeq)
Code explanation
Line 2: Define a recursive function
lucasSeq()to create a Lucas sequence and pass the terms count as a parameter.Lines 4–.7: Define the base case for
indexvalues 0 and 1.Line 9: Calculate the subsequent term by recursively calling the
lucasSeq()with index - 1 and index - 2 as parameters and adding the returned values.Line 12: Set an
indexvalue that represents the number of Lucas sequence terms we want to output on the console.
Line 13: Create a list comprehension
mySeqand call the recursive functionlucasSeq()for eachivalue in the definedrange().Line 14: Print the obtained Lucas sequence on the console using
print().
Memoization approach
In this example, we will optimize the recursive approach by using memoization which is often helpful in scenarios where the problem at hand is being solved multiple times.
#memoization approachdef lucasMemo(index, memo={}):if index in memo:return memo[index]if index == 0:return 2if index == 1:return 1memo[index] = lucasMemo(index - 1, memo) + lucasMemo(index - 2, memo)return memo[index]#Driver codelength = 8mySeq = [lucasMemo(i) for i in range(length)]print(mySeq)
Code explanation
Line 2: Define a recursive function
lucasMemo()to create a Lucas sequence and pass the terms count and a memo dictionary as parameters.Lines 3–8: Define the base case for
indexvalues 0 and 1 and for the value existing in the memo dictionary.Line 9: Calculate the subsequent term by recursively calling the
lucasMemo()with index - 1 and index - 2 along with the memo dictionary as parameters and adding the returned values.Line 10: Return the value in the memo dictionary on the specified
indexvalue once the recursive calls are executed and returned.
Line 13: Set a
lengthvalue that represents the number of Lucas sequence terms we want to output on the console.Line 14: Create a list
mySeqand call the recursive functionlucasMemo()for eachivalue in the definedrange().Line 15: Print the obtained Lucas sequence on the console using
print().
Comparing complexities
Method | Time Complexity | Space Complexity |
Iterative approach | O(n) | O(1) |
Recursive approach | O(2n ) | O(n) |
Memoization approach | O(n) | O(n) |
Summary
All three approaches work well to obtain a Lucas sequence; however iterative approach has space complexity
Free Resources