Advertisement
AlaminSakib

Untitled

Nov 13th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. There is a Swiss bank, which has many lockers in their underground vault. The lockers are numbered from 1 to N, initially all locked. A genius thief somehow managed to enter the vault, and now wants to open all the lockers with his magical key.
  2.  
  3. Now this thief is kind of crazy. Instead of opening all the lockers serially from 1 to N, he decides to open them in multiple passes!
  4.  
  5. On the first pass, he opens the first unopened locker (which is always locker #1 in the first pass), and every second unopened lockers thereafter.
  6.  
  7. On the second pass, he opens the first unopened locker, and every third unopened lockers thereafter.
  8.  
  9. So basically, on the nth pass, he opens the first unopened locker, and every (n+1)th unopened lockers thereafter.
  10.  
  11. Say, there are 9 lockers.
  12.  
  13. On the first pass - he opens 1, 3, 5, 7, and 9. At this stage, these lockers are still closed: 2, 4, 6, and 8.
  14.  
  15. On the second pass - he opens 2 and 8. At this stage, these lockers are still closed: 4 and 6.
  16.  
  17. On the third pass - he opens locker 4. At this stage, only locker 6 is still closed.
  18.  
  19. On the final pass, he opens locker 6. Now all the lockers are open.
  20.  
  21. Input
  22.  
  23. A number N (1<=N<=2000) representing the number of lockers.
  24.  
  25. Output
  26.  
  27. Print the number of the locker opened last.
  28.  
  29. Two Sample Inputs
  30.  
  31. 9
  32.  
  33. 42
  34.  
  35. Two Sample Outputs
  36.  
  37. 6
  38.  
  39. 42
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement