In: Computer Science
PYTHON
The nth term of the sequence is given by: Xn = (2 ∗ Xn-3) +Xn-2+/ + (4 ∗ Xn-1)
For example, if the first three values in the sequence were 1, 2 and 3. The fourth value would be: (2 * 1) + 2 + (4 * 3) = 16 Replacing the previous three Integers (2, 3 and 16) in the formula would determine the fifth value in the sequence. The nth term can be calculated by repeating this process. Write a program that includes two functions main() and calculate()which are used to determine the nth term in the sequence.
The main() function: • Displays a description of the program’s function • Prompts the user for the number of values in the sequence (n) • Prompts the user for the first three values in the sequence • Using a loop, calls the calculate() function repeatedly until the nth value is determined • Displays the nth value of the sequence
The calculate() function: • Accepts three values as arguments • Calculates the next value in the sequence • Returns the calculated value
Pseudo code
1) Take the input for the value of n
2) Take first 3 input values
3) iterate in loop from 0 to n-3
4) call the calculate function and append the returned value to the list
5) print the result
// Code
def calculate(a,b,c):
return 2*a+b+4*c # calculation
# Main Driver Code
print("Enter Number of vlaues in the sequence:")
n=int(input()) #it takes the input for the value of n
print("Enter first 3 values:")
li=list(map(int,input().split())) # it takes first three
values
for i in range(0,n-3): # loop to calculate values
li.append(calculate(li[i],li[i+1],li[i+2])) # function call
print(str(n)+"th value of the series is "+str(li[-1]))
// OUTPUT
Enter Number of vlaues in the sequence:
4
Enter first 3 values:
1 2 3
4th value of the series is 16
...Program finished with exit code 0
Press ENTER to exit console.
// Code Images
//OUTPUT IMAGE