# A building with an odd elevator

In a building, there is an odd elevator, which has only two buttons: UP, which makes it go up 9 floors, and DOWN, which makes it go down 7 floors. (The ground floor is floor 0.) It is possible to reach each floor, but if the building was just one floor less tall, this would not be possible anymore. How many floors are there in this building?

• (I just made up this puzzle, so bear with me if it is well-known) – mau May 25 '14 at 11:27
• This problem is a variant of the chicken nugget problem. But congratulations on adding your own little twist! – Joe Z. May 25 '14 at 19:49
• @JoeZ.: I thought at that problem, and indeed both of them are based on Euclid's algorithm. However I could not find a natural way to relate their solutions... but probably I should ask in math.SE for help! – mau May 25 '14 at 21:36
• Instead of buying packages of 6 or 9 chicken nuggets, you're buying 9 and selling 7 (and you can't have more than 15 at once). – Joe Z. May 25 '14 at 21:37
• Pardon me. Do you agree that in a 28-floor building I may reach any floor from 0 to 15? If this is the case, what can stop me from arriving to floor 13 and then perform the same algorithm to reach any floor from 13 to 13+15? – mau May 29 '14 at 21:42

15             --v
14             . .           --v
13         --v . .           . .
12         . . . .       --v . .
11     --v . . . .       . . . .
10     . . . . . .   --v . . . .
9  --v . . . . . .   . . . . . .
8  . . . . . . . --v . . . . . .
7  . . . . . . .   . . . . . . --
6  . . . . . --^   . . . . . .
5  . . . . .       . . . . --^
4  . . . --^       . . . .
3  . . .           . . --^
2  . --^           . .
1  .               --^
0  ^


An intuitive explanation for this is that you can only go two floors up at a time, and you have to get above floor 6 to go down and reach an odd numbered floor. But every time you go up by two, you also travel to the floor 7 floors above that one. Therefore, the minimum amount of floors for this to work is [smallest even number > 6] + 7, which is 8+7=15.

By the same logic, a general formula can be constructed, where n = the greater number (9) and m is the lesser:

$$\text{[smallest number > m divisible by n-m]} + m$$

From which this slightly easier formula can be made:

$$m - (m \text{ mod } (n-m)) + (n-m) + m$$

Which can be simplified to:

$$m + n - (m \text{ mod } (n-m))$$

• just a note: knowing that British and American buildings start at a different floor, I stated that ground floor is 0, so the answer is 15 :-) Besides, the formula I found is much simpler! – mau May 25 '14 at 12:18
• @mau Heh, that would have been good to know. :-P edited – Doorknob May 25 '14 at 12:21