Question

In: Computer Science

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
           opCount+=2;
       }
       opCount++;
       System.out.println("f(N) = [f(N)= 3N + 4]");
       System.out.println("O(N) = [O(N)=O(N)]");
       System.out.println("OpCount : "+opCount);
       return sum; // 1
   }

Solutions

Expert Solution

//do comment if any problem arises

//highlighted code is your function

//there are some bugs in given code according to formula f(N)= 3N + 4 which has been fixed

import java.util.Scanner;

class Sum {

public static long sum1(int N)// f(N) = ; O(N) =

{

long opCount = 0;

long sum = 0; // 1

for (int i = 0; i < N; i++) // 2 + 2N

{

sum++; // N

opCount += 3;

}

opCount++;

opCount++;

opCount++;

opCount++;

System.out.println("f(N) = [f(N)= 3N + 4]");

System.out.println("O(N) = [O(N)=O(N)]");

System.out.println("OpCount : " + opCount);

return sum; // 1

}

public static void main(String[] args) {

Sum object = new Sum();

Scanner sc = new Scanner(System.in);

System.out.print("Enter value of N: ");

int N = sc.nextInt();

System.out.println("O(N) = " + object.sum1(N) + "\n");

}

}

Output:


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++;            }   ...
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...
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...
YOU MUST SHOW ALL WORK (CALCUATIONS) AND DRAW TIME LINES. WRITE WITH THE CORRECT FORMULA TO...
YOU MUST SHOW ALL WORK (CALCUATIONS) AND DRAW TIME LINES. WRITE WITH THE CORRECT FORMULA TO BE USED 1) Jane is 25 years old and was able to save $25,000.    After doing some research, she identifies a stock called HPG Industries that has a dividend yield of 7% that has been consistent for the past 10 years. Based on this information, she decides to invest in this stock. Considering that HPG Industries pays the dividend consistently, how many years will...
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...
Answer & show work andswer and show work answer n show work Using a sample of...
Answer & show work andswer and show work answer n show work Using a sample of 20 people, the testing agency found that 14 of them had better protection than that provided by the competitor. Do you have enough evidence to say/claom that your suncreen lotion provides better protection than the competitiors in a majority of cases? Use alpha = 0.01 to answer. 1. What are the apporiate hypotheses for situation? 2. the appropriate rejection rule is? 3. the calculated...
For the given array, simulate the working operation of Bubble Sort. Show your work at each...
For the given array, simulate the working operation of Bubble Sort. Show your work at each step. Make sure to show the status of the array after every swap. [ 28, 13, 22, 7, 34, 2 ]
Let f(x)=x • 3^x a) Find formula for f^(n) •(x) for natural n (the n order...
Let f(x)=x • 3^x a) Find formula for f^(n) •(x) for natural n (the n order derivative). b) Write the Taylor series generated by f(x) in 0.
Find a closed formula for each of the following sequences. Show all work and explain your...
Find a closed formula for each of the following sequences. Show all work and explain your answers. (a) {1, 6, 17, 34, 57, 86, 121, . . .}, where a0 = 1. (b) an = 5an−1 + 4, a0 = 2 (c) an = 10an−1 − 21an−2, a0 = 6, a1 = 26.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT