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 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++;
           }
       }
       System.out.println("f(N) = [your answer]");
       System.out.println("O(N) = [your answer]");
       System.out.println("OpCount : "+opCount);
       return sum;
   }
   /*
  
   * Add operation counts
   * f(N) formula (show your work)
   * O(N) reduction
   */
   public static long sum5(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       long sum = 0; //1
       for(int i = 0; i < N; i++) //2N + 2
       {
           for(int j = 0; j < i*i; j++) //N^2 + N + 1
           {
               for(int k = 0; k < j; k++) //
               {
                   sum++;
               }
           }
       }
       System.out.println("f(N) = [your answer]");
       System.out.println("O(N) = [your answer]");
       System.out.println("OpCount : "+opCount);
       return sum;
   }
   /*
  
   * Add operation counts
   * f(N) formula (show your work)
   * O(N) reduction
   */
   public static long sum6(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       long sum = 0;
       for(int i = 1; i < N; i++)//i starts at 1 to prevent division error in if statement
       {
           for(int j = 0; j < i*i; j++)
           {
               if(j%i == 0)
               {
                   for(int k = 0; k < j; k++)
                   {
                       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

public static long sum4(int N)//f(N) = ; O(N) =
{
long opCount = 0;
long sum = 0; //
for(int i = 0; i < N; i++) //runs N times
{
for(int j = 0; j < i; j++) //runs i times, possible i values are 0,1,2,3,,,,,N-1
{
sum++;
}
}
System.out.println("f(N) = [your answer]");
System.out.println("O(N) = [your answer]");
System.out.println("OpCount : "+opCount);
return sum;//
}
total run time f(n): 0+1+2+3+4+...+N-1 = N*(N-1)/2
f(n)=N*(N-1)/2
O(N)=O(N^2)


public static long sum5(int N)//f(N) = ; O(N) =
{
long opCount = 0;
long sum = 0; //1
for(int i = 0; i < N; i++) //runs N times
{
for(int j = 0; j < i*i; j++) //runs i*i times, possible value for i:0,1,2,3,,,,N-1
{
for(int k = 0; k < j; k++) //runs j times, possible values for j:0,1,4,8,,,, (N-1)*(N-1)
{
sum++;
}
}
}
System.out.println("f(N) = [your answer]");
System.out.println("O(N) = [your answer]");
System.out.println("OpCount : "+opCount);
return sum;
}
inner most loop runs j times for every iteration of outer loop
middle loop runs runs i*i times for every iteration of outer loop
outer loop runs N times
now
i=0:
-j=0:
--k=0;//denotes number of times it runs
i=1:
-j=0:
--k=0;
i=2:
-j=0,1,2,3:
--k=0+1+2+3 ;
i=3:
-j=0,1,2,3,4,5:
--k=0+1+2+3+4+5;
this means
for each i value below two loop run time will be : i*i*(i*i-1)/2
so total complexity f(n): 0*0*(0*0-1)/2 + 1*1(1*1-1)/2 +2*2*(2*2 -1)/2 +....+(N-1)*(N-1)((N-1)*(N-1)-1)/2
F(N) = 0+0+4(4-1)/2 +9*(9-1)/2 +....+ (N-1)^2 * ((N-1)^2 -1)/2
O(N) = O(N^5)

public static long sum6(int N)//f(N) = ; O(N) =
{
long opCount = 0;
long sum = 0;
for(int i = 1; i < N; i++)//i starts at 1 to prevent division error in if statement
{
for(int j = 0; j < i*i; j++)
{
if(j%i == 0)//the number of times below loop executed is : i times only
{//condition satified for i times
for(int k = 0; k < j; k++)
{
sum++;
}
}
}
}
System.out.println("f(N) = [your answer]");
System.out.println("O(N) = [your answer]");
System.out.println("OpCount : "+opCount);
return sum;
}

f(N) = 1*(1+1)/2 + 2*(2+1)/2 +....+ (N-1)*(N-1)/2
O(N) = O(N^4)



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 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++;           ...
   * 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