Q1: Write a java program to sum (1 + 2 + . . . + n ) for any positive integer n?
ID: 3877998 • Letter: Q
Question
Q1:
Write a java program to sum (1 + 2 + . . . + n ) for any positive integer n?
Q2:
Algorthem A: sum = 0
for i = 1 to n
sum = sum + i.
Algorthem B:
sum = 0
for i = 1 to n
{
for j = 1 to i
sum = sum +1
}
Algorthem C:
sum = n * (n + 1) / 2
Write a Java program that implements the three algorithms and times them for various values of n
.Test your code with n=1000, n=10000, n=100000, n=1000000, ...
A: 1+2+3+...+n
B: 1+(1+1)+(1+1+1)+...
C: Hint:
Option1: long
startMilliseconds = System.currentTimeMillis ();
Option2: Date
current = new Date();longstartTime=current.getTime();
Q3:
Using Big Oh notation, what is the order of the following computation’s time requirement?
for i = 1 to n
{
for j = 1 to 5
Sum = sum + 1
}
Explanation / Answer
Que1: Create a file named code.java and paste following code into it! Flename must be code.java
code.java
public class code
{
public static void main(String[] args)
{
int n = 10;
int sum = 0;
for(int i = 1; i<= n;i++)
{
sum = sum + i;
}
System.out.println("Sum of first " + n + " numbers = " + sum);
}
}
Sample Output:
Sum of first 10 numbers = 55
Que2: Create a file named code2A.java and paste following code into it! Flename must be code2A.java
code2A.java
public class code2A
{
public static void main(String[] args)
{
int n = 1000000;
int sum = 0;
long startMilliseconds = System.currentTimeMillis ();
for(int i = 1; i<= n;i++)
{
sum = sum + i;
}
long endMilliseconds = System.currentTimeMillis ();
long time = endMilliseconds - startMilliseconds;
System.out.println("Time to run the program when n = " + n + ": " + time + " millisecs");
}
}
Sample Output:
Time to run the program when n = 1000000: 4 millisecs
Que2: Create a file named code2B.java and paste following code into it! Flename must be code2B.java
code2B.java
public class code2B
{
public static void main(String[] args)
{
int n = 10000;
int sum = 0;
long startMilliseconds = System.currentTimeMillis ();
for(int i = 1; i<= n;i++)
{
for(int j = 1; j<=i ; j++)
{
sum = sum + 1;
}
}
long endMilliseconds = System.currentTimeMillis ();
long time = endMilliseconds - startMilliseconds;
System.out.println("Time to run the program when n = " + n + ": " + time + " millisecs");
}
}
Sample Output:
Time to run the program when n = 10000: 29 millisecs
Que2: Create a file named code2C.java and paste following code into it! Flename must be code2C.java
code2C.java
public class code2C
{
public static void main(String[] args)
{
int n = 1000;
int sum = 0;
long startMilliseconds = System.currentTimeMillis ();
sum = n*(n + 1)/ 2;
long endMilliseconds = System.currentTimeMillis ();
long time = endMilliseconds - startMilliseconds;
System.out.println("Time to run the program when n = " + n + ": " + time + " millisecs");
}
}
Sample Output:
Time to run the program when n = 1000: 0 millisecs
Que3:
Outer loop runs n times and inner loop runs 5 times.
So, number of operations = 5*n
So time complexity = O(5n) = O(n)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.