Question

Part 1 The function 'vbrf' (for very bad recursive function) is defined as follows: vbrf(0) =...

Part 1

The function 'vbrf' (for very bad recursive function) is defined as follows:

vbrf(0) = 2
vbrf(1) = 3
vbrf(2) = 4
vbrf(3) = 5
vbrf(n) = vbrf(n-1) - vbrf(n-2) + 3*vbrf(n-3) - 2*vbrf(n-4) if n > 3

Your job for this problem is to implement the function vbrf as a method in Java. Then write a program that uses this method to compute the function for various values of the argument and time how long it takes to compute. Also, include in this program a part that computes the function for the arguments from 1 to 12 and displays the argument and the results. Once you have the program working and can examine the results, report the following in a comment near the beginning of your program file:

  • Identify the function and describe a much quicker way to compute it.
  • Identify the smallest value of the argument for which it takes at least 10 seconds to compute the value of vbrf. Report the argument value and how long the function took, in seconds.

To determine how long it takes to compute vbrf, use code something like the following:

long t0 = System.nanoTime();
int x = vbrf(15);
long t1 = System.nanoTime();
double timeInSeconds = (t1-t0)*1.0e-9;
            

Be aware, the time it takes to compute vbrf(n+1) is almost twice the time it takes to compute vbrf(n). Start with small values of n and work your way up.

Part 2

We will be studying one aspect of permutations in Assignment #5. We will make use of a function that is defined recursively. Your job for this part of the assignment is to implement this function and print the first few values. Implement the method as a static method that takes a long for an argument and returns a long. The method itself should do no printing.

We'll call the function h. It is defined by these conditions:

h(0) = 1
h(1) = 0
h(n) = (n-1)*(h(n-1) + h(n-2)) for n > 1

To check your work, here are the first few values of h:

n h(n)
0 1
1 0
2 1
3 2
4 9
5 44
6 265
7 1854
8 14833
9 133496
10 1334961
11 14684570
12 176214841
13 2290792932
14 32071101049
15 481066515734

Your program should print the first 10 values or so of the function. Your program should not be interactive!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class h{

public static void main(String[] args) {
int b = 6;
int c = h(b);
System.out.println("answer= " + c);
}

/** returns the minimum of two numbers */
public static int h(int n) {
int val;
val=(n-1)*(h(n-1)+h(n-2));

return val;
}

}

Add a comment
Know the answer?
Add Answer to:
Part 1 The function 'vbrf' (for very bad recursive function) is defined as follows: vbrf(0) =...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • Problemi Consider the plecewise function sit) defined as follows: -(+2 when -2sts2 0.5 elsewhere s(t)- Write...

    Problemi Consider the plecewise function sit) defined as follows: -(+2 when -2sts2 0.5 elsewhere s(t)- Write a MATLAB user-defined function called aystfun.n that has one input argument to, and two output arguments: s0 and vsO. Your function must satisfy all the following requirements: 1. Your function must calculate and return the vector so - s(t0) for any supplied numerical vector to. We assume that the function is always called with a vector t0 whose elements are all in [-4,4]. 2....

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • Suppose that a Tibonacci sequence is defined as follows: Tibo(0) = 1, Tibo(1) = 2, Tibo(2)...

    Suppose that a Tibonacci sequence is defined as follows: Tibo(0) = 1, Tibo(1) = 2, Tibo(2) = 3 Tibo(n) = Tibo(n-1) + Tibo(n-3), when n ≥ 3 Write a recursive method Tibo, that takes some integer n as a parameter and returns the n-th Tibonacci number. You DO NOT need to write the main function.

  • The indicator function of a set, A, is denoted by l and is defined as follows...

    The indicator function of a set, A, is denoted by l and is defined as follows (a) Show that if A and B are any two sets, then the indicator function, ln, of (b) IfA and B are disjoint (i.e An B is empty), show that luB is the sum, 1, if x E A A n B, is the product lalB.i.e Show that lanB (x) =ム(x)h(x) for every x. ム+1B. İ.e Show that 1AUB (x)-4(x) + 1B (x) for...

  • IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up...

    IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up the factorial of the natural numbers from 1 to N can be expressed as The recursive algorithm: N-1 N-2 N-3 Write independent matlab code to solve the above problem with the following methods: 1. 2. 3. A monolithic program (with no functions) A standard (non-recursive) user defined function (an a program to call it) A recursive function (an a program to call it) Test...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Suppose that the function h is defined on the interval (-2, 2] as follows. -1 if...

    Suppose that the function h is defined on the interval (-2, 2] as follows. -1 if -2<xs-1 0 if - 1<xs0 h(x) = 3 1 if 0<xs1 2 if 1<xs2 Find h(-1), h(0.25), and h (2). h (0.25) = | х ó ? h (2) = 0 Egnation Check Mac

  • Complete count_bits function. You are given an 32-bits integer stored in $t0. Count the number of...

    Complete count_bits function. You are given an 32-bits integer stored in $t0. Count the number of 1's in the given number. For example: 1111 0000 should return 4 j main ############################################################### # Data Section .data # new_line: .asciiz "\n" space: .asciiz " " double_range_lbl: .asciiz "\nDouble range (Decimal Values) \nExpected output:\n1200 -690 104\nObtained output:\n" swap_bits_lbl: .asciiz "\nSwap bits (Hexadecimal Values)\nExpected output:\n75757575 FD5775DF 064B9A83\nObtained output:\n" count_bits_lbl: .asciiz "\nCount bits \nExpected output:\n20 24 13\nObtained output:\n" swap_bits_test_data: .word 0xBABABABA, 0xFEABBAEF, 0x09876543 swap_bits_expected_data: .word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT