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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started