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(kn), where k is the number of digits of n.

Solutions

Expert Solution


So this question is based on math.

Let me explain how?

We all know every number has some factors that divide that number or (n%k ==0) where k is the factor of n

for example

10 = {1,2,5,10} these 2 the factors of 10

9 = {1,3,9) these 3 are the factors of 9 ->perfect square

so let's solve one with an example and see what answer comes

N = 5

step 0:1 2 3 4 5

0 0 0 0 0 // initially all are closed

step 1:1 2 3 4 5

1 1 1 1 1 so 1 is a factor of every number and everyone is open now and 1 is also a perfect square

step 2:1 2 3 4 5

1 0 1 0 1 2 is factor of 2 and 4

step 3:1 2 3 4 5

1 0 0 0 1 3 is a factor of 3 only so it closes it

step 4:1 2 3 4 5

1 0 0 1 1 // 4 is factor of 4 so it opens it 4->perfect square

step 5:1 2 3 4 5

1 0 0 1 0 // 5 is factor if 5 so it closes it

so, at last, we see there are 2 boxes that are open 1,4

if we look closely 1 and 4 are a perfect square having only three factors 4 = {1,2,4}

so this is our answer any number that is perfect square will remain open at the last

perfect squares are {1,4,9,16,25,36,49..........and so on}

so I will be writing a code for it with time complexity O(log2(N) )


#include <iostream>

using namespace std;
int open_boxex(int n){
int count = 0;
for(int i = 1;i*i<=n;i++){ // this loop will run only log(n) times so time complexity is O(log(n))
if(i*i<=n){ // checking for perfect square
count++; // if perfect square then increment the count
}
}
return count; // return number of boxes that are open
}
int main()
{
cout<<"Enter the number of boxes\n";
int n;
cin>>n;
cout<<"open boxes are :"<<open_boxex(n);
// n = 4 = {1,4,9,16} so answer would be 4;

return 0;
}

if any doubt, comment below


Related Solutions

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,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT