Write a Python program using functions and recursion to generate and display the first 15 numbers of the Padovan sequence. The Padovan sequence begins with the first three terms as 1, 1, and 1, and each subsequent term is the sum of the previous two terms. For example, the first few terms of the Padovan sequence are: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37,.Note: The Padovan sequence is a sequence of numbers similar to the Fibonacci sequence, where each term is the sum of the previous two terms.
Question
Write a Python program using functions and recursion to generate and display the first 15 numbers of the Padovan sequence. The Padovan sequence begins with the first three terms as 1, 1, and 1, and each subsequent term is the sum of the previous two terms. For example, the first few terms of the Padovan sequence are: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37,.Note: The Padovan sequence is a sequence of numbers similar to the Fibonacci sequence, where each term is the sum of the previous two terms.
Solution
Sure, here is a Python program that uses recursion to generate the first 15 numbers of the Padovan sequence:
def padovan(n):
if n == 0 or n == 1 or n == 2:
return 1
else:
return padovan(n-2) + padovan(n-3)
for i in range(15):
print(padovan(i))
Here's how it works:
- The function
padovan(n)is defined to calculate the nth number in the Padovan sequence. - If n is 0, 1, or 2, the function returns 1 because the first three numbers in the Padovan sequence are all 1.
- If n is greater than 2, the function returns the sum of the (n-2)th and (n-3)th numbers in the sequence. This is done by recursively calling the
padovan()function with argumentsn-2andn-3. - A for loop is used to print the first 15 numbers in the Padovan sequence. The loop variable
iranges from 0 to 14, and for each value ofi, theith number in the Padovan sequence is printed.
Similar Questions
Given a number n, find the nth number in the Padovan Sequence.A Padovan Sequence is a sequence which is represented by the following recurrence relationP(n) = P(n-2) + P(n-3)P(0) = P(1) = P(2) = 1Note: Since the output may be too large, compute the answer modulo 10^9+7.
Create a program that generates and prints the Fibonacci series up to a specified number 'N'. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers, starting with 0 and 1. Your program should take an integer input 'N' and display the Fibonacci series up to the Nth term using a while loop.Fibonacci series: 0, 1, 1, 2, 3, 5, 8,... Note: This question is one of the most asked questions in placements.Input format :The input consists of a positive integer N.Output format :The output displays the Fibonacci series up to the Nth term separated by space.
Helen is developing a program for a gaming application that involves generating a sequence of mystical numbers based on the Tribonacci series. She needs to implement a recursive function tribonacci to determine the Tribonacci numbers for various stages of the quest. Write a program to achieve her task.The Tribonacci series is a sequence of numbers defined as the sum of the three preceding terms. 0, 1, 1, 2, 4, 7, 13, and so on.Input format :The input consists of a positive integer n.Output format :The output displays the n terms in the Tribonacci series, separated by a space.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 20Sample test cases :Input 1 :2Output 1 :0 1 Input 2 :9Output 2 :0 1 1 2 4 7 13 24 44 Input 3 :18Output 3 :0 1 1 2 4 7 13 24 44 81 149 27
Generate the next three terms for the following number sequences and give an appropriate name to the sequence.a.1,4,9,16,25,36,___,___,___1,4,9,16,25,36,___,___,___+ Workspace+ Check answerb.1,1,2,3,5,8,13,___,___,___1,1,2,3,5,8,13,___,___,___+ Workspace+ Check answerc.1,8,27,64,125,___,___,___1,8,27,64,125,___,___,___+ Workspace+ Check answerd.2,3,5,7,11,13,17,___,___,___2,3,5,7,11,13,17,___,___,___+ Workspace+ Check answere.4,6,8,9,10,12,14,15,___,___,___4,6,8,9,10,12,14,15,___,___,___+ Workspace+ Check answerf.121,131,141,151,___,___,___121,131,141,151,___,___,___+ Workspace+ Check answer
Instead of storing the entire Padovan sequence in a list, you can optimize the space complexity by just storing the last three elements of the sequence. This way, you can calculate the next element based on these three elements without storing the entire sequence.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.