Question

java data structure question - Consider the following recurrence function" f(1)=1; f(2)=2; f(3)=3; f(4)=2; f(5)=2 f(n)...

java data structure question - Consider the following recurrence function"

f(1)=1; f(2)=2; f(3)=3; f(4)=2; f(5)=2

f(n) = 2 * f(n -1) + f(n – 5) for n >= 6

  1. Write a program to demonstrate this recurrence relation (recursive method) and demonstrate it for the values n = 6, 7, 10, and 12;

  2. Create a second method that solves the same problem using an iterative solution. Modify your test program to show that both the recursive method and the iterative methods produce the same results.

Homework Answers

Answer #1

java code

public class MyClass {
    public static void main(String args[]) {
      // recurive
      System.out.println("Using recursive function");
      System.out.println("At n=6 f(6) = " + recursive(6));
      System.out.println("At n=7 f(7) = " + recursive(7));
      System.out.println("At n=10 f(10) = " + recursive(10));
      System.out.println("At n=12 f(12) = " + recursive(12));
      
      System.out.println(" ");
      // iterative function calling
      System.out.println("using iterative function");
      System.out.println("At n=6 f(6) = " + iterative(6));
      System.out.println("At n=7 f(7) = " + iterative(7));
      System.out.println("At n=10 f(10) = " + iterative(10));
      System.out.println("At n=12 f(12) = " + iterative(12));
    }
    
    // recurssive function
    public static int recursive(int n)
   {
      if (n == 1)
        return 1;
      else if (n == 2)
        return 2;
      else if (n == 3)
        return 3;
      else if (n == 4)
        return 2;
      else if (n == 5)
        return 2;
      else
        return 2*recursive(n-1)+recursive(n-5);
      
   }
   
    // Iterative function
    public static int iterative(int n)
   {
      int arr[]; 
      arr = new int[n]; 
      arr[0]=1;
      arr[1]=2;
      arr[2]=3;
      arr[3]=2;
      arr[4]=2;
      int i = 6;
      while(i<=n)
      { arr[i-1] = 2*arr[i-1-1] + arr[i-1-5] ;
        i++;
          
      }
      
      return arr[n-1];
   }
}

output screenshot

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider below recurrence relation f_(n )=f_(n-1)+ f_(n-2) for n ≥ 3. f_(1 )=1 and f_2 =...
Consider below recurrence relation f_(n )=f_(n-1)+ f_(n-2) for n ≥ 3. f_(1 )=1 and f_2 = 3 (a) Please compute the first seven numbers in this sequence. (b) Find the closed form for this recurrence relation. Solving the characteristic equation, and solving for constants
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes F n using the recursive definition given above. ii. Describe a more efficient algorithm to calculate F n and give its running time.
Consider the following recursive equation s(2n) = 2s(n) + 3; where n = 1, 2, 4,...
Consider the following recursive equation s(2n) = 2s(n) + 3; where n = 1, 2, 4, 8, 16, ... s(1) = 1 a. Calculate recursively s(8) b. Find an explicit formula for s(n) c. Use the formula of part b to calculate s(1), s(2), s(4), and s(8) d Use the formula of part b to prove the recurrence equation s(2n) = 2s(n) + 3
Master Theorem: Let T(n) = aT(n/b) + f(n) for some constants a ≥ 1, b >...
Master Theorem: Let T(n) = aT(n/b) + f(n) for some constants a ≥ 1, b > 1. (1). If f(n) = O(n logb a− ) for some constant > 0, then T(n) = Θ(n logb a ). (2). If f(n) = Θ(n logb a ), then T(n) = Θ(n logb a log n). (3). If f(n) = Ω(n logb a+ ) for some constant > 0, and af(n/b) ≤ cf(n) for some constant c < 1, for all large n,...
solve the non-homogenous recurrence relation for an = 2an-1+an-2-2an-3+8.3n-3 where   a0 = 2, a1 = 6...
solve the non-homogenous recurrence relation for an = 2an-1+an-2-2an-3+8.3n-3 where   a0 = 2, a1 = 6 ve a2=13 Find characteric equation by plugging in  an = rn try to solve general solution and solve nonhomogeneous particular solution and find total final answer please.. My book anwer is A(1)n+B(-1)n+C(2)n+k3n , A=1/2, B=-1/2, C=1 ve k=1. can you give me more explain about this please..?
f:N->N f(1)=3 and f(2)=5 and for each n>2, f(n)=f(n-1)+f(n-2). Please prove that for each n in...
f:N->N f(1)=3 and f(2)=5 and for each n>2, f(n)=f(n-1)+f(n-2). Please prove that for each n in N, f(n)*f(n+2)=(f(n+1))^2+(-1)^(n) using induction.
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: 1. Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that...
Here is some empirical data: { 1, 2, 1, 4, 5, 3, 1, 2, 2, 1,...
Here is some empirical data: { 1, 2, 1, 4, 5, 3, 1, 2, 2, 1, 1, 3, 5, 1, 3, 2, 1, 2, 5, 2, 4, }. Create a Python program that draws a random number from the empirical distribution of this data
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT