Question

In: Computer Science

/*    * Add operation counts    * f(N) formula (show your work)    * O(N)...

/*
   * Add operation counts
   * f(N) formula (show your work)
   * O(N) reduction -
   */
   public static long sum2(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       long sum = 0;
       for(int i = 0; i < N; i++)
       {
           for(int j = 0; j < N; j++)
           {
               sum++;
           }
       }
       System.out.println("f(N) = [your answer]");
       System.out.println("O(N) = [your answer]");
       System.out.println("OpCount : "+opCount);
       return sum;
   }

Solutions

Expert Solution

/*
 * Add operation counts
 * f(N) formula (show your work)
 * O(N) reduction -
 */
public static long sum2(int N)//f(N) = ; O(N) =
{
    long opCount = 0;
    long sum = 0;
    opCount++;  // for long sum = 0
    for (int i = 0; i < N; i++) {
        // so, number of operations in the below for loop = 3N
        for (int j = 0; j < N; j++) {
            sum++;
            opCount += 3;   // one for j++, one for j < N, one for sum++
        }
        // number of operations here = 4
        opCount += 2;   // one for extra j < N, one for int j = 0
        opCount += 2;   // one for i++, one for i < N
    }
    opCount += 2;   // one for extra i < N, one for int i = 0
    // so, total number of operations = (3N+4)*N + 2 = 3N^2 + 4N + 3
    /**
     * Outer for loop iterates N times
     * Inner for loop iterates N times
     * As explained in above comments, number of operations = 3N^2 + 4N + 3
     * so, f(N) = N^2
     * and time complexity is O(N^2)
     */
    System.out.println("f(N) = 3N^2 + 4N + 3");
    System.out.println("O(N) = O(N^2)");
    System.out.println("OpCount : " + opCount);
    return sum;
}

Related Solutions

* Add operation counts -    * f(N) formula (show your work) -    * O(N)...
* Add operation counts -    * f(N) formula (show your work) -    * O(N) reduction -    */    public static long sum4(int N)//f(N) = ; O(N) =    {        long opCount = 0;        long sum = 0; // 1        for(int i = 0; i < N; i++)        {            for(int j = 0; j < i; j++)            {                sum++;   ...
   * Add operation counts    * f(N) formula (show your work)    * O(N) reduction...
   * Add operation counts    * f(N) formula (show your work)    * O(N) reduction    */    public static long sum3(int N)//f(N) = ; O(N) =    {        long opCount = 0;        long sum = 0;        for(int i = 0; i < N; i++)        {            for(int j = 0; j < N*N; j++)            {                sum++;            }   ...
Does this look correct? Add operation counts - 0.5pt    * f(N) formula (show your work)...
Does this look correct? Add operation counts - 0.5pt    * f(N) formula (show your work) - 0.5pt    * O(N) reduction - 0.5pt public static long sum1(int N)//f(N) = ; O(N) =    {        long opCount = 0;        long sum = 0; //1        opCount++;        opCount++;        opCount++;        for(int i = 0; i < N; i++) //2 + 2N        {            sum++; //N       ...
1.)Prove that f(n) = O(g(n)), given: F(n) = 2n + 10; g(n) = n 2.)Show that...
1.)Prove that f(n) = O(g(n)), given: F(n) = 2n + 10; g(n) = n 2.)Show that 5n2 – 15n + 100 is Θ(n2 ) 3.)Is 5n2 O(n)?
Correctly follow the described algorithm to complete the method    * Add operation counts -   ...
Correctly follow the described algorithm to complete the method    * Add operation counts -    * f(N) formula (show your work) -    * O(N) reduction -    */    public static int[] algorithm1(int N)//f(N) = ; O(N) =    {        long opCount = 0;        int[] arr = new int[N];        /*        * Use the following method to fill the array        * For each position in the array, generate a...
If f(n) = 3n+2 and g(n) = n, then Prove that f(n) = O (g(n))
If f(n) = 3n+2 and g(n) = n, then Prove that f(n) = O (g(n))
Use Excel to show your work and include the formula in the cell to show how...
Use Excel to show your work and include the formula in the cell to show how you arrived at your figures. Round percentages (example if 49.2, round to 49). Background: ABC, Inc., produces widgets. The company manufactures three levels of widgets-Economy, Better and Best. Selected information on the widgets is given below. Economy Better Best Selling price per widget $40.00 $60.00 $90.00 Variable expense per widget production $22.00 $27.00 $31.50 Selling (5% of selling price) $2.00 $3.00 $4.50 All sales...
Show that 2nlog(n) + 10n + 3log(n) + 6 is O(nlog(n)). Justify your answer by providing...
Show that 2nlog(n) + 10n + 3log(n) + 6 is O(nlog(n)). Justify your answer by providing a real-valued constant c > 0, corresponding to the upper-bound constant factor, and an integer constant n0 ≥ 1, consistent with the definition of Big-Oh
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
Please show all your work step by step and all the formula that is used. This...
Please show all your work step by step and all the formula that is used. This is my 3rd time posting as people do not explain what they are doing. Therefore, do not answer if you can't explain. D. What is the present value (PV) of a 12-years lease arrangement with an interest rate of 7.5% that requires annual payments of $4250. Per year with first payment being due now? E. A recent college graduate hopes to have $200000. Saved...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT