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".
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence....
Write a recursive and an iterative function to calculate the nth element in a Fibonacci sequence. A Fibonacci sequence is defined as the element 1, followed by another 1, and each element thereafter is the sum of the previous two elements. For example, the first 9 elements of a Fibonacci sequence are: 1 2 3 5 8 13 21 34 This famous sequence was originally used to predict the growth of rabbit populations! Once you have each of the functions...
** * Write a recursive function that removes the first k even numbers * from the...
** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack<Integer> stack, int k) { return 0;...
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)...
Is there any way to do this function //Recursive method to check if two numbers form...
Is there any way to do this function //Recursive method to check if two numbers form a given number private static boolean addition(int[] array, int start, int end, int n) { if(start < end) { if (array[start] + array[end] == n) { return true; } if (array[start] + array[end] > n) { return addition(array, start, end-1, n); } if (array[start] + array[end] < n) { return addition(array, start+1, end, n); } } return false; }
Generate 5th order linear recursive sequence using shift registers using the primitive polynomial 1+X^2+x^5
Generate 5th order linear recursive sequence using shift registers using the primitive polynomial 1+X^2+x^5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT