1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Volgvan
3 years ago
8

Consider the following algorithm: ```c++ Algorithm Mystery(n) { // Input:A nonnegative integer n S = 0; for i = 1 to n do { S =

S + i * i; } return S } ``` 1. What does this algorithm compute? 2. What is its basic operation? 3. How many times is the basic operation executed? 4. What is the efficiency class of this algorithm? 5. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done.
Computers and Technology
1 answer:
jasenka [17]3 years ago
3 0

Answer:

See explanation

Explanation:

First let us find what this algorithm compute:

Algorithm Mystery(n) {

        S = 0;

        for i = 1 to n do

             { S = S + i * i; }

         return S }

1)

Let suppose n = 3

S = 0

The algorithm has a for loop that has a loop variable i initialized to 1

At first iteration:

i = 1

S = S + i * i;

   = 0 + 1 * 1

S = 1

At second iteration:

i = 2

S = 1

S = S + 2 * 2;

   = 1 + 2 * 2

   = 1 + 4

S = 5

At third iteration:

i = 3

S = 5

S = S + 3 * 3;

   = 5 + 3 * 3

   = 5 + 9

S = 14

Now the loop breaks at i=4 because loop iterates up to n and n =3

So from above example it is clear that the algorithm computes the sum of squares of numbers from 1 to n. Or you can say it compute the sum of first n squares.  Let us represent this statement in mathematical form:

∑\left \ {{n} \atop {i=1}} \right. i²

2)

From above example we can see that the basic operation is multiplication. At every iteration loop variable i is multiplied by itself from 1 to n or till n times. However we can also say that addition is the basic operation because at each iteration the value of S is added to the square of i. So it takes the same amount of time.

3)

In the for loop, the basic operation executes once. We can say that at each iteration of the loop, multiplication is performed once. Suppose A(n) represents the number of times basic operation executes then,

A(n) = ∑\left \ {{n} \atop {i=1}} \right. 1 = n

4)

Since for loop executes once for each of the numbers from 1 to n, so this shows that for loop executes for n times. The basic operation in best, worst or average case runs n times. Hence the running time of Θ(n) . We can say that A(n) = n ∈ Θ(n)  

If b is the number of bits needed to  represent n then

b = log₂n + 1

b = log₂n

So

n ≈ 2^{b}

A(n) ≈ 2^{b} ≈ Θ( 2^{b} )  

5)

One solution is to calculate sum of squares without using Θ(n) algorithm.  So the efficient algorithm that takes less time than the previous algorithm is:

Algorithm Mystery(n) {

        S = 0;

        S = (n * ( n + 1 ) (2n + 1) ) / 6 ;

        return S}

Now the sum can be calculated in Θ(1)  times. This is because regardless of the size and number of operands/operations, the time of arithmetic operation stays the same. Now lets find out how this algorithm works for

n = 3

S = (n * ( n + 1 ) (2n + 1) ) / 6 ;

 = (3 * ( 3 + 1 ) * (2(3) + 1) ) / 6

 = (3 * (4) * (6+1)) / 6

= (3 * (4) * (7)) / 6

= (3 * 4 * 7) / 6

= 84 / 6

S = 14

You might be interested in
Discuss the types of data that business might collect and how the business could use that data to drive decision-making in a spe
Rudik [331]

Answer:

Answered below

Explanation:

A business such as an online store like Amazon can collect user data like name, address, mouse clicks on products, how long users stay on page viewing a particular product, marital status, education and many more.

These data are used by these businesses to drive decision-making that enhances the sale of their goods. For instance, adverts and search alternatives about a particular good can be shown to people who have looked at them before. A newly married or pregnant woman would be shown baby products etc.

3 0
3 years ago
Consider the recursive method myprint in this code snippet: public void myprint(int n) { if (n < 10) { system.out.print(n); }
valkas [14]
What the method does, is it takes the rightmost decade and prints it, then divides by 10 and repeats. Effectively this means the number is printed backwards, so the output is 128.
7 0
3 years ago
Which of the following statements are true about the Internet?
Arlecino [84]
1, 2, and 3 are true
6 0
3 years ago
A __________ note is a private note that you leave for yourself or for other people who might use the presentation file
BaLLatris [955]

Answer:

The answer is that it is a speaker note.

Explanation:

It leaves a note for people that use presentation files. I use it all the time on my google slides.

7 0
2 years ago
The following program is run. Then the user clicks the "bottomButton" TWO TIMES. What will be displayed in the console?
IrinaK [193]

Answer:

A. aaa

bbb

ccc

ccc

ddd

B. bbb

ddd

ccc

ccc

C. bbb

ddd

aaa

aaa

D. bbb

ccc

ccc

ddd

8 0
3 years ago
Read 2 more answers
Other questions:
  • Machine language library routines are installed on computers Select one: a. because they can come as part of the operating syste
    15·1 answer
  • Where do today's computers store almost all motherboard configuration data?
    15·1 answer
  • Finding your style in photography is about ___
    10·2 answers
  • The ____ dialog box in windows vista appears each time a user attempts to perform an action that can be done only with administr
    12·1 answer
  • HTTP is made to facilitate which kind of communication?
    11·1 answer
  • Your browsing the Internet and realize your browser is not responding which of the following will allow you to immediately exit
    14·2 answers
  • What will be the value of ans after the following code has been executed?
    10·2 answers
  • A digital media professional's computer is too full of video files. She would like to upgrade her computer to have more capacity
    12·2 answers
  • Write a regular expression that selects lines containing any of the following words: linux windows solaris macos. For this exerc
    13·1 answer
  • Which of the following uses the proper syntax for creating an HTML comment?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!