Knowee
Questions
Features
Study Tools

Single File Programming QuestionProblem StatementHelen 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 274 504 927 1705 3136 5768 10609 Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

Question

Single File Programming QuestionProblem StatementHelen 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 274 504 927 1705 3136 5768 10609 Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution for the problem:

def tribonacci(n):
    if n==1:
        return 0
    elif n==2 or n==3:
        return 1
    else:
        return tribonacci(n-1) + tribonacci(n-2) + tribonacci(n-3)

n = int(input())
for i in range(1, n+1):
    print(tribonacci(i), end=' ')

This program works by defining a recursive function tribonacci(n) that calculates the nth number in the Tribonacci series. The base cases are defined for n=1, n=2, and n=3. For n>3, the function calls itself with the arguments n-1, n-2, and n-3, and returns their sum.

The main part of the program reads an integer n from the input, then prints the first n numbers in the Tribonacci series by calling the tribonacci(i) function in a loop from 1 to n. The numbers are printed on the same line, separated by spaces.

Please note that this solution has a high time complexity due to the repeated calculation of the same Tribonacci numbers. For larger inputs, a more efficient solution would be to use dynamic programming to store the calculated Tribonacci numbers and reuse them when needed.

This problem has been solved

Similar Questions

Problem StatementHelen 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

Single File Programming QuestionProblem StatementImagine Mary is developing a program to calculate the digital root of a positive integer. The digital root of a positive integer is the number obtained by repeatedly summing the digits until a one-digit sum is obtained. For example, for the number 873597, the digital root is calculated in the following steps:-> 8 + 7 + 3 + 5 + 9 + 7 = 39 -> 3 + 9 = 12 -> 1 + 2 = 3 Write a program that will read in a positive integer and write both its digital root and the number of steps required to obtain it.Note: This question was asked in ACM-ISPC contest.Input format :The input consists of a positive integer n.Output format :The first line displays "Digital Root: " followed by the digital root of n.The second line displays "Number of Steps: " followed by the number of steps required to reach a single-digit digital root.Refer to the sample output for the formatting specificationsCode constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ n ≤ 106Sample test cases :Input 1 :123Output 1 :Digital Root: 6Number of Steps: 1Input 2 :2Output 2 :Digital Root: 2Number of Steps: 0Input 3 :873597Output 3 :Digital Root: 3Number of Steps: 3Input 4 :1000000Output 4 :Digital Root: 1Number of Steps: 1

Single File Programming QuestionProblem StatementJustin needs a program to generate and analyze the Hailstone Sequence for a specific positive number up to 1. Hailstone Sequences follow these rules: If a number is even, divide it by 2. If a number is odd, multiply it by 3 and add 1.Help Justin by writing a program using a function to calculate the Hailstone sequence.For example: If the input number is 13, the calculations are as follows:13 (Odd): 3 * 13 + 1 = 4040 (Even): 40 / 2 = 2020 (Even): 20 / 2 = 1010 (Even): 10 / 2 = 55 (Odd): 3 * 5 + 1 = 1616 (Even): 16 / 2 = 88 (Even): 8 / 2 = 44 (Even): 4 / 2 = 22 (Even): 2 / 2 = 1Hence the sequence is 13 40 20 10 5 16 8 4 2 1 and the length of the sequence is 10. Note: This question helps in clearing technical coding tests for service-based companies.Input format :The input consists of a positive integer n, representing the starting number for the Hailstone Sequence.Output format :The first line displays the Hailstone Sequence starting from n up to 1, separated by a space.The second line displays "The length of the sequence is X." where X is the length of the generated sequence.

Single File Programming QuestionProblem StatementNancy, an aspiring programmer, has a series of alphabets from A-Z arranged circularly. Write a program that takes an integer input representing the position in the series and prints the corresponding character. Example: If Miranda inputs 1, it corresponds to 'A'.If Miranda inputs 28, it corresponds to 'B' because the alphabet series is circular, and position 28 loops back to the second letter in the series.Help Nancy by crafting a program that meets her challenge using a recursive function called generateTerm.Input format :The input consists of an integer n, representing the position for which the character needs to be retrieved,Output format :The output prints the character present at the given position.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 ≤ 100Sample test cases :Input 1 :1Output 1 :AInput 2 :28Output 2 :BInput 3 :59Output 3 :GInput 4 :100Output 4 :VNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

Single File Programming QuestionProblem StatementJessi wants to write a program where a recursive function, recursiveFunc, is called with an input integer n. Inside the function, a local variable m is initialized to 3 using the auto keyword. The program prints the value of m before and after each recursive call. The input integer n is obtained from the user in the main function.Input format :The input consists of an integer n.Output format :The output prints the value of the variable m before and after each recursive call from n to 0:The format for before call: "Before call(x): m" where x is the number of the particular call.The format for after call: "After return(x): m" where x is the number of the particular call.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 ≤ 10Sample test cases :Input 1 :1Output 1 :Before call(1): 3Before call(0): 3After return(0): 3After return(1): 3Input 2 :5Output 2 :Before call(5): 3Before call(4): 3Before call(3): 3Before call(2): 3Before call(1): 3Before call(0): 3After return(0): 3After return(1): 3After return(2): 3After return(3): 3After return(4): 3After return(5): 3Input 3 :10Output 3 :Before call(10): 3Before call(9): 3Before call(8): 3Before call(7): 3Before call(6): 3Before call(5): 3Before call(4): 3Before call(3): 3Before call(2): 3Before call(1): 3Before call(0): 3After return(0): 3After return(1): 3After return(2): 3After return(3): 3After return(4): 3After return(5): 3After return(6): 3After return(7): 3After return(8): 3After return(9): 3After return(10): 3Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

1/3

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.