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
.When one component modifies an internal data item in anothercomponent we call this:
Lady bird [3.3K]

Answer:

A. content coupling

Explanation:

When one component modifies an internal data item in another component we call this content coupling.

5 0
3 years ago
What are the possible consequences of intentional virus setting?
natali 33 [55]

<u>The possible consequences of intentional virus setting:</u>

Virus is nothing but desktop or laptop or tablet OS has to do certain process as on its own which diverse other  to different process  such us stealing the data from desktop or laptop or tablet and making slow the performance of desktop and laptop  and tablet.

To avoid or protect the computer there are two type of antivirus.

  • On desktop or tablet or laptop. (It is called resident firewall).
  • Internet gateway. (It is called firewall)

So company create virus and to rectify they make money of that process.

To protect our desktop or laptop or tablet from virus we are protected with law. In case due to virus attacked our laptop or desktop or tablet Is effected we can fine the or sent to jail by law.

3 0
3 years ago
What is the proper order for the fetch-execute cycle?
vesna_86 [32]

Control Unit – controls all parts of the computer system. It manages the four basic operations of the Fetch Execute Cycle as follows:

Fetch – gets the next program command from the computer’s memory

Decode – deciphers what the program is telling the computer to do

Execute – carries out the requested action

Store – saves the results to a Register or Memory

Arithmetic Logic Unit (ALU) – performs arithmetic and logical operations

Register – saves the most frequently used instructions and data

5 0
3 years ago
I WILL GIVE BRAINLIEST TO WHO ANSWERS FIRST AND CORRECTLY.
bogdanovich [222]

Answer:

True

Explanation:

3 0
3 years ago
What is the best motivation that you can do/give to make your employees stay? ​
Lorico [155]

i'd give fringe benefits to them every month

Explanation:

to encourage them to work on the mission statement and the business goal

8 0
3 years ago
Other questions:
  • Physical access, security bypass, and eavesdropping are examples of how access controls can be ________.
    15·1 answer
  • Can someone text me cuss I'm bored my number is (225) 975 7120
    10·1 answer
  • A(n) ____ is a system designed to handle only very basic applications with an absolute minimum amount of hardware required by th
    9·1 answer
  • Which network topology requires terminators at the ends of the backbone cable?
    6·1 answer
  • If you want to prioritize downloads of your mobile app instead of visits to your mobile site, you should: a) add a sitelink exte
    10·1 answer
  • Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the
    5·1 answer
  • Select the correct answer.
    6·2 answers
  • A External hackers have some access to a company's website and made some changes. Customers have submitted multiple complaints v
    11·1 answer
  • 25 Points Asap <br> Write a Java program named Light.java that displays a light bulb shown below:
    14·1 answer
  • a domain name is assigned to you when you submit your website to a search engine. group of answer choices true false
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!