Question: public static double algorithm2(int n ) { long t1= System. nanoTime( ); long sum = (long )(n/2)(n+1); double average = (double) sum / (double) n;

public static double algorithm2(int n ) { long t1= System. nanoTime( ); long sum = (long )(n/2)(n+1); double average = (double) sum / (double) n; long t2 = System. nanoTime( ); System.out.println("Sum of numbers from 1 to 1000000 (equation) ="+ sum); System.out.println( Average of numbers from 1 to 1000000 (equation) = " + average); System.out.println("Execution time in nanoseconds equation: " + (t2 - t1)); return average; \} Find Complexity Analysis \& Execution time (show complexity calculation): Assume that System.out.println is only 1 instruction complexity for value of n=1000,000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
