Question: Q1: Write a java program to sum (1 + 2 + . . . + n ) for any positive integer n? Q2: Algorthem A:

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 computations time requirement?

for i = 1 to n

{

for j = 1 to 5

Sum = sum + 1

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!