Riddle Description:
\$N\$ doors are closed. In the first pass, I open all of them. In the second pass, I toggle every second door. In the third pass, I toggle every third door. I continue this until I have completed the \$N\$th pass. Find all the doors that will remain open after \$N\$ passes.
public static void printDoorsOpened(int passes)
{
HashMap<Integer, Boolean> doors=new HashMap<Integer, Boolean>();
int i = 0;
//Close all doors as default in the start
while(i < passes)
{
doors.put(i, false);
i++;
}
for(int j = 1; j <= passes; j++)
{
for(int k = j - 1; k < passes; k += j)
doors.put(k, !(doors.get(k)));
}
for(int l = 0; l < passes; l++)
{
System.out.println("The door number "+(l + 1)+" is "+(doors.get(l) == true ? "Opened" : "Closed"));
}
}
Please feel free to review it based on best practices, style and any other efficient solution, but please refrain from solely style-based reviews.