Cells and Prisoners
Thousands of years ago, a king called Samudragupta ruled the lands of Saurashtra. He was called the king of oceans as it was almost impossible for anyone to capture his lands through the battles on seas. Once a great king from Greece, Philip II used the sea routes to capture Samudragupta. Fortunately or unfortunately Samudragupta captured Philip II and his 788 powerful army. Each captive was put into a cell starting from cell number 1. The prisoners were provided sufficient food, air and water but they had to live in dark rooms. A prisoner's number was his cell number. Philip II was in cell number 173.
On the eve of the king Samudragupta's birthday, he decided to free some of the prisoners according to a plan. Initially all the prisoners were taken out of their respective cells. The doors of all the cells were closed. According to the plan:
Prisoner 1 has to open the doors of all the cells.
Prisoner 2 has to close the doors of the cells 2, 4, 6, ...
Prisoner 3 has to visit the cells 3, 6, 9, ...
If the doors are open, he has to close.
If the doors are closed, he has to open.
Similarly, continuing we have
Prisoner i has to visit all the cells i, 2i, 3i, ...
If the doors are open, he has to close.
If the doors are closed, he has to open.
This will be continued till the last captive.
At last, the prisoners of the cells whose doors were open would be released.
The queen was very eager to know how many prisoners shall be released and whether the captured king himself be released or not. Do you have the answer for the queen's questions before the plan could be executed?
Hint 1
Hint 2
Answer
Solution