Question

In: Computer Science

Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose...

Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose one different from that of any posted thus far. Write a recursive method that given n, computes the nth term of that sequence. Also provide an equivalent iterative implementation. How do the two implementations compare?

Solutions

Expert Solution

Implementation for ex

Recursive Solution:

float seriesRecursive(int x, int n) {
       if(n == 0)
           return 1;
       return (((float)x / n) * seriesRecursive(x, n - 1));      
   }

Iterative Solution:
   float seriesIterative(int x, int n) {
       int fact = 1, fx = 1;
       for(int i = 1; i < n; i++) {
           fx *= x;
           fact *= i;
       }
       return (float)fx / fact;
      
   }

Comparison:

Every iterative code can be converted into recursive code and vice-versa. Usually recursive code length is less than iterative code (less code). But recursive code has more space complexity than iterative one and that is because recursive code uses stack to call method recursively and hence extra space is needed. However both takes same time complexity usually.


Related Solutions

Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Determine if each of the following recursive definition is a valid recursive definition of a function...
Determine if each of the following recursive definition is a valid recursive definition of a function f from a set of non-negative integers. If f is well defined, find a formula for f(n) where n is non-negative and prove that your formula is valid. f(0) = 1, f(n) = -f(n-1) + 1 for n ≥ 1 f(0) = 0, f(1) = 1, f(n) = 2f(n-1) +1 for n ≥ 1 f(0) =0, f(n) = 2f(n-1) + 2 for n ≥...
Show that any polynomial over C (the complex numbers) is the characteristic polynomial of some matrix...
Show that any polynomial over C (the complex numbers) is the characteristic polynomial of some matrix with complex entries. Please use detail and note any theorems utilized.
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
Determine whether each of these proposed definitions is a valid recursive definition of a function f...
Determine whether each of these proposed definitions is a valid recursive definition of a function f from the set of nonnegative integers to the set of integers. If f is well defined, find a formula for f (n) when n is a nonnegative integer and prove that your formula is valid. e) f (0) = 2, f (n) = f (n − 1) if n is odd and n ≥ 1 and f (n) = 2f (n − 2) if...
Determine whether each of these proposed definitions is a valid recursive definition of a function f...
Determine whether each of these proposed definitions is a valid recursive definition of a function f from the set of nonnegative integers to the set of integers. If f is well defined, find a formula for f(n) when n is a nonnegative integer and prove that your formula is valid. a) f(0) = 1, f(n) = -f(n-1) for n ≥ 1 b) f(0) = 1, f(1) = 0, f(2) = 2, (n) = f(n-3) for n ≥ 3. c) f(0)...
Choose one construct to make a scale on. Provide a conceptual definition of such a construct....
Choose one construct to make a scale on. Provide a conceptual definition of such a construct. Provide a brief description of individuals who “possess” the construct or those who may score high on a scale measuring the construct and a person who might score low on the same construct.
Please state these definition and use detal and espilon: (fn) is a sequence if function: 1.What...
Please state these definition and use detal and espilon: (fn) is a sequence if function: 1.What is the definition of (fn) is continuous? 2.What is the definition of (fn) is uniformly continuous? 3.What is the difference between the function continuous and the sequence of function continuous?
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and...
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and returns the maximum increase from one element in the sequence to an element at a higher index. Assumptions and restrictions: The function must return a number. If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements. You can assume that the argument...
Problem 3 1. Choose either the Halton or the Sobol sequence of quasi-random numbers. Briefly describe...
Problem 3 1. Choose either the Halton or the Sobol sequence of quasi-random numbers. Briefly describe how they are constructed. 2. Illustrate graphically the difference between pseudo-random numbers and quasi-random numbers. 3. Repeat step 2 of Problem 1 with quasi-random numbers. Comment.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT