Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program: The function \'vbrf\' (for v ery b ad r ecursive f unction) is def

ID: 640834 • Letter: J

Question

Java Program:

The function 'vbrf' (for very bad recursive function) is defined as follows: No user input.

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:

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

Your program should print the first 10 values or so of the function.

Final display:

Explanation / Answer

//Java program that calculates and prints
//10 vbrf function values with time taken
//to calculate each value
//VbrfFunction.java
public class VbrfFunction
{
   public static void main(String[] args)
   {
      
      
       System.out.printf("%-5s%-10s%-10s ","n","h(n)","Time(nano)");
       //print the vbrf funtion values for 10 integers with time taken
       //for each value to calculate
       for (int i = 0; i <=10; i++)
       {
           long t0 = System.nanoTime();
           int x = vbrf(i);
           long t1 = System.nanoTime();
           double timeInSeconds = (t1-t0)*1.0e-9;
           //print n and h(n) and time taken for each integer value
           System.out.printf("%-5d%-10d%-10f ",i,x,timeInSeconds);
       }
   }
  
   //Recursive funtion to calculate vbrf
   public static int vbrf(int n)
   {
       if(n==0)
           return 1;
       else if(n==1)
           return 0;
       else
           //call the metod vbrf function
           return (n-1)*(vbrf(n-1) + vbrf(n-2));

   }
  
}


----------------------------------------------------------------------------------------

Sample output:

n    h(n)      Time(nano)
0    1         0.000003
1    0         0.000000
2    1         0.000001
3    2         0.000001
4    9         0.000001
5    44        0.000001
6    265       0.000002
7    1854      0.000002
8    14833     0.000003
9    133496    0.000005
10   1334961   0.000007

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote