Home‎ > ‎ProgComp‎ > ‎ProgComp 2015‎ > ‎

Task 1. 64 Doors

Task 1. 64 Doors

Available Marks: 7

In a monastery in a faraway country there is a chamber with 64 doors numbered 1 to 64. Every year the 64 monks that live in the monastery participate in the following ritual.

All doors in the chamber are initially closed. Each monk is assigned a unique integer between 1 and 64. The monks then enter the chamber one at a time, in numeric order (though that doesn't affect the outcome). If a monk is assigned the integer k, he changes the state of every k-th door. That is, if the door is closed he opens it and if it's open he closes it.

So the first monk changes every door (from closed to open), the second changes doors #2, #4, #6 etc, the third changes #3, #6, #9 etc, and so on until the last monk, who just changes #64.

At the end of the ritual the doors that are currently open lead to (spiritual) treasures, so their numbers are significant.

Your task

Write a program that simulates the ritual and displays the numbers of the doors that are open at the end, on one line.


Task adapted from: Rosetta Code. Image source: info.xfactorllc.com

Comments