Random posts about coding

Mostly blogging about dart.

Ways to Solve the N Doors N Passes Question

| Comments

Question

1000 doors in a row that are initially closed. 1000 passes on the doors. Each time you visit a door you toggle it. If open->close, if close->open. First time you visit every door, second time you visit every other door, third time you visit every 3rd door, etc.. until visiting all 1000 doors. How many doors are left open at the end? Which are open, which are closed? What is unique about the sequence left open?

Solutions

The O(n2) solution to the problem which presents interesting results in its output.

The O(n) solution that takes advantage of a known identity of perfect squares found in the problem.