Question

In: Computer Science

write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?

write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?

Solutions

Expert Solution

# Python program to create a function that calculates the value of an at n

# given a0 = 1 and a1 = 2 and an = an-1 * an-2 , n > 1

# function to calculate the value of an at n

def find_an(n):

              

               if n == 0: # if n=0 return 1

                              return 1

               elif n == 1: # if n=1 return 2

                              return 2

               else: # return the product of function call with n-1 and n-2

                              return find_an(n-1)*find_an(n-2)

              

# call and print the value of an at n=5

print(find_an(5))

#end of program

Code Screenshot:

Output:


Related Solutions

Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of a mod n. Your program should allow user to enter a and n. Note: For this question please make sure the code compiles and runs, it is not copied and pasted from elsewhere( I will be checking!). Thanks
1. (2 pts each) Consider the following algorithm: procedure polynomial(c, a0,a1,…an: real numbers) power≔1 y≔a0for i=1...
1. (2 pts each) Consider the following algorithm: procedure polynomial(c, a0,a1,…an: real numbers) power≔1 y≔a0for i=1 to n   power≔power*cy≔y+ai*power return y (Note: y=ancn + an-1 cn-1 +. . . + a1C +a0 so the final value of y is the value of the polynomial at x=c) a. Use the algorithm to calculate f(3), where f(x)=2x2+3x+1 at x=3. Show the steps of working through the algorithm – don’t just plug 3 in for x in f(x). b. How many multiplications and...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are...
What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are at least 40 lines of comments and 40 lines of code (1 point) b) the problem is described at the top of the program in a readable fashion (1 point) c) the code includes at least one instance of each of the five learning objectives listed above. (3 points) d) the code solves the problem. This may be assessed in part by a 'code...
What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are...
What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are at least 40 lines of comments and 40 lines of code (1 point) b) the problem is described at the top of the program in a readable fashion (1 point) c) the code includes at least one instance of each of the five learning objectives listed above. (3 points) d) the code solves the problem. This may be assessed in part by a 'code...
Using C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0...
Using C++ 1) Write a program that repeatedly evaluates a n-th order polynomial p(x) = a0 + a1*x + a2*x^2 + ... + an*x^n where n <= 10 The program inputs n, ai, and x from the keyboard and then prints out the corresponding value of p to the screen. The program continues to input new values of n, ai, x and evaluates p until a negative value for n is input, at which point the program stops.
2. Write the hexadecimal numbers in the registers of $a0, $a1, $a2, $a3 after the following...
2. Write the hexadecimal numbers in the registers of $a0, $a1, $a2, $a3 after the following codes running: ori $a0, $0, 11 ori $a1, $0, 19 addi $a1, $a1, -7 slt $t2, $a1, $a0 beq $t2, $0, label addi $a2, $a1, 0 sub $a3, $a1,$a0 j end_1 label: ori $a2, $a0, 0 add $a3, $a1, $a0 end_1: xor $t2, $a1, $a0 *Values in $a0, $a1, $a2, $a3 after the above instructions are executed.
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
1. Define the elements of the following equation: P = a0 ‒ a1 × Qd. 2....
1. Define the elements of the following equation: P = a0 ‒ a1 × Qd. 2. Given P = $150 ‒ 0.005 × Qd as the demand for a professional sports team: a. If P = $60, what is Qd? b. If P = $40, what is Qd? 3. Imagine these two possible changes from the demand curve listed in Question 2: a. P = $175 ‒ 0.005 × Qd b. P = $125 ‒ 0.005 × Qd For each,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT