Question

In: Computer Science

Consider a set of n boxes with their positions numbered from 1 to n. Initially all...

Consider a set of n boxes with their positions numbered from 1 to n. Initially all the boxes
are CLOSED. These n boxes are subject to modifications over n iterations as follows. At the ith
iterations the boxes at the positions whose ids are multiples of i are flipped, i.e. changed from
CLOSED to OPEN or vice-versa. For example, at iteration 1, boxes at all positions are
flipped, at iteration 2, boxes at positions 2,4,6,etc. are flipped, at iteration 3, boxes at positions 3,6,9, etc.
are flipped.
Propose an algorithm for find out, that after n such iterations how many boxes will be OPEN.
Note that the answer requires only how many boxes, not their positions. Describe the rationale
for the algorithm, give the pseudocode, and its complexity. Grading scheme: (your grade will be
based on whichever class your algorithm falls in)
- For algorithms with complexity O(n2) or higher.
- For algorithms with complexity O(kn), where k is the number of digits of n.
- For algorithms with complexity O(M(k)), where k is the number of digits of n, and M(k) is
the complexity of multiplying two k digit numbers.

Solutions

Expert Solution

Lets consider an example where n=24 boxes. So, we will visit the box for its every divisor i.e. 1 and 24, 2 and 12, 3 and 8, 4 and 6. Initially it is closed. So at 1, box will be flipped i.e opened and closed at divisor '2'. It will be again opened at '3' and closed at '4', opened at '6' and closed at '8' and opened at '12' and closed at '24'. So for every pair of divisors the box will end up just as in its initial state.

But this is not always the case. Consider n=16 boxes. for n= 16, the divisors are 1 and 16, 2 and 8, 4 and 4. But we see that '4' is repeated i.e. we have odd number of divisors here because 16 is a perfect square. So the box number 16  will open for 1 and closed for 2, opened for '4' and closed for '8' and again opened for '16'. So, the boxes which are perfect squares will remain opened.

Hence, for a set of 'n' boxes, we will need to find the number of perfect squares from 1 to 'n' which will give the number of boxes opened.

Algorithm :-

Step 1:- Initialize a variable 'i' to '1' . Take a count variable 'count' and initialize it with '0'.

Step 2:- calculate i*i and store it in a variable 'x' i.e. x=i*i.

If x<=n, increase count and i by 1 i.e, count++ and i++. and repeat step 2.

else go to step 3.

Step 3:- 'count' variable will give the number of perfect squares from 1 to n

PseudoCode for it:-

int i=1;

int count =0;

while (i*i<=n)

x=i*i;

if(x<=n) {

count ++; i++; }

else

break;

print("No of boxes opened i.e. number of perfect squares = ",count)

Time complexity :-

This algorithm falls under the third category i.e  o(M(k)), as we have k digits number for every 'i' in while loop and we are multiplying the k-digit number by itself i.e. two times. And we know that M(k) is the complexity of multiplying two k-digit number.

If you like the answer, please support by upvoting it. And if you have any doubts regarding this, please comment in the comment section, I am sure to help.


Related Solutions

Consider a collection of 10 empty boxes (numbered from 1 to 10) and 5 balls. Suppose...
Consider a collection of 10 empty boxes (numbered from 1 to 10) and 5 balls. Suppose that each ball is placed in a box chosen at random. Assume that this placement of a ball in a box is performed independently for each ball. Note that a box may contain more than one ball and that some of the boxes will necessarily remain empty. Hint: Since more than one ball can be placed in the same box, it is best to...
Consider two boxes of ideal gas. The boxes are thermally isolated from the world, and initially...
Consider two boxes of ideal gas. The boxes are thermally isolated from the world, and initially from each other as well. Each box holds N molecules in volume V. Box #1 starts with temperature Ti,1 while #2 starts with Ti,2. (The subscript “i” means “initial,” and “f” will mean “final.”) So, the initial total energies are Ei,1 = (3/2)NkBTi,1 and Ei,2 = (3/2)NkBTi,2. Now we put the boxes into thermal contact with each other, but still isolated from the rest...
Consider the following game. There are 10 political positions, 1, · · · , 10, from...
Consider the following game. There are 10 political positions, 1, · · · , 10, from the most liberal to the most conservatives. Two politicians simultaneously choose what position they pick in order to maximize the percentage of votes they can get. 10% of voters are at each position. Voters will vote for the politician they are closer to. If two politicians choose the same position then they each get 50% of the votes. (a) Solve this game by iterative...
1.) How many relations are there from a set of size n to a set of...
1.) How many relations are there from a set of size n to a set of size m? 2.) Determine the number of entries in the following sequences: a.) {13, 19, 25, . . . , 601} b. {7, 11, 19, 35, 67, . . . , 131075}
Let “ ·n” be multiplication modulo n, and consider the set Un = { [a] ∈...
Let “ ·n” be multiplication modulo n, and consider the set Un = { [a] ∈ Zn | there is a [b] ∈ Zn with [a] ·n [b] = [1]} (a) Show that (Un, ·n ) is a group. (b) Write down the Cayley table for U5. Hint: |U5| = 4. (c) Write down the Cayley table for U12. Hint: |U12| = 4.
Consider the following subsets of the set of all students: A = set of all science...
Consider the following subsets of the set of all students: A = set of all science majors B = set of all art majors C = set of all math majors D = set of all female students Using set operations, describe each of the following sets in terms of A, B, C, and D: a) set of all female physics majors b) set of all students majoring in both science and art
(1) Show that the set { 1 m + 1 n : m, n ∈ N}...
(1) Show that the set { 1 m + 1 n : m, n ∈ N} is countable. (2) Show that the set {a + b √ 2 : a, b ∈ Q} is countable. (3) Show that the intersection of two countable sets is countable. (4) Show that the set of all irrational numbers is uncountable. (5) Let C = {0, 1, 2, . . . , 9}. Show that the set C ×C × · · · is...
Consider two urns of balls: the first contains 5 different red balls numbered from 1 to...
Consider two urns of balls: the first contains 5 different red balls numbered from 1 to 5 and the second contains 4 different blue balls numbered from 1 to 4. You are asked to pick one ball from the first urn (i.e., the one with red balls) and one ball from the second urn (i.e., the one with blue balls). Each outcome has the form (r, b), where r denotes the number on the red ball and b denotes the...
Spectral Absorption and Emission Consider a hydrogen atom with an electron initially in the n =...
Spectral Absorption and Emission Consider a hydrogen atom with an electron initially in the n = 4 state. a) List the 3 different wavelengths that could be observed in the emission spectrum for this atom given the electron begins in level n = 4. b) What is the longest wavelength photon that could potentially be absorbed by the electron in the level n = 4? c) How much energy would be required to ionize this atom? d) What is the...
1. The following set of data is from a sample of n = 8            171...
1. The following set of data is from a sample of n = 8            171   169   158   164   158   135   150 141 a) Find the mean, median, and mode. b) Calculate the variance, standard deviation, range, inter-quartile range, and coefficient of variation. c) Describe the shape of the data. d) List the five-number summary and build a boxplot.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT