Calculate how many trips you have to make to get to the desired floor.

Victor lives in a 20-storey building. The elevator at its entrance is out of order, so only two buttons work in the car. When you click on one of them, the elevator rises 13 floors, when you click on the other, it goes down to 8. How can Victor get from the 13th floor to the 8th floor to a friend?

The problem can be solved in different ways. Let's look at the classic way first.

The elevator cannot go beyond the boundaries of the floors. If Victor, being on the 13th floor, presses the "Up" button, the elevator will not reach the 26th floor, because there is simply no elevator in the house. It turns out that Victor will have to go down:

1. 13 − 8 = 5.

From the 5th floor he will only be able to go up, because there is no “minus 3” floor in the house either. This means that Victor can go up or down only if the number of floors allows it. That is, he always has one option, which button to press. You get the following travel history:

2. 5 + 13 = 18.

3. 18 − 8 = 10.

4. 10 − 8 = 2.

5. 2 + 13 = 15.

6. 15 − 8 = 7.

7. 7 + 13 = 20.

8. 20 − 8 = 12.

9. 12 − 8 = 4.

10. 4 + 13 = 17.

11. 17 − 8 = 9.

12. 9 − 8 = 1.

13. 1 + 13 = 14.

14. 14 − 8 = 6.

15. 6 + 13 = 19.

16. 19 − 8 = 11.

17. 11 − 8 = 3.

18. 3 + 13 = 16.

19. 16 − 8 = 8.

In 19 trips, Victor will finally reach the floor where his friend is waiting for him.

Now let's look at a more vital way.

Most often, the elevator reaches the top or bottom floor and stops, regardless of how many more floors it has to drive. Victor can take advantage of this and get to his friend faster. Here's how it would be:

1. 13 − 8 = 5.

2. 5 - 8 = 1 (the lift reached the 1st floor and stopped, it cannot go below).

3. 1 + 13 = 14.

4. 14 − 8 = 6.

5. 6 + 13 = 19.

6. 19 − 8 = 11.

7. 11 − 8 = 3.

8. 3 + 13 = 16.

9. 16 − 8 = 8.

Voila! Victor got to the right floor in 9 trips. Much better than 19!