TopCoder problem "LockersDivOne" used in Member SRM 482 (Division I Level One)



Problem Statement

    

A hallway is filled with lockers numbered 1 through N, initially all closed. Out of boredom, Dave and Earl decide to open all the lockers. They make multiple passes through the hallway, each beginning at locker 1. On the first pass, they open the first unopened locker, and every second unopened locker thereafter. On the second pass, they open the first unopened locker, and every third unopened locker thereafter. In general, on the nth pass, they open the first unopened locker, and every (n+1)th unopened locker thereafter.

For example, with 9 lockers, on the first pass they open 1, 3, 5, 7, and 9, leaving 2, 4, 6, and 8. On the second pass they open 2 and 8, leaving 4 and 6. On the third pass they open locker 4, and on the final pass locker 6.

You will be given N, the number of lockers. Return the number of the locker opened last.

 

Definition

    
Class:LockersDivOne
Method:lastOpened
Parameters:int
Returns:int
Method signature:int lastOpened(int N)
(be sure your method is public)
    
 

Constraints

-N will be between 1 and 2000000, inclusive.
 

Examples

0)
    
9
Returns: 6
The example from the problem statement.
1)
    
42
Returns: 42
2)
    
314
Returns: 282

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=11110

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=14235&pm=11110

Writer:

pieguy

Testers:

ivan_metelsky , boba5551 , it4.kp , abal9002

Problem categories:

Brute Force, Simulation