Draft

Open the Lockers

Description:

Problem

There is a line of lockers numbered 1 to N, initially all closed. A man walks down the line, opens 1, and then alternately skips and opens each closed locker (i.e. he opens 1, skips 2, opens 3, skips 4, and so on).

At the end of the line, he turns around, and walks: opening the first closed locker he reaches, and alternately skips and opens each closed locker. He does this again (walking forward, then backward, then forward, ...), until all the lockers are opened.

Return a function LastLocker(N) that will return the number of the locker opened last.

Input / Output

Input: Any positive integer N (no need to check it)
Output: Integer (the last locker)

Source

Shoutout to our professors in maths class for introducing to us this problem. I don't know who actually made this problem (probably from Putnam / local Maths Olympiad), but this actually is fascinating -- for me at least.

Mathematics
Puzzles

Stats:

CreatedJun 21, 2016
Warriors Trained80
Total Skips18
Total Code Submissions145
Total Times Completed22
Python Completions22
Total Stars4
% of votes with a positive feedback rating81% of 13
Total "Very Satisfied" Votes10
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes2
Total Rank Assessments12
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • regginator729 Avatar
Ad