Imprisoned Logicians

Two friends, logicians - Ein and Stein, get imprisoned in two distant cells in a castle. Both cells have just one door, and a window with 8 bars in the first cell, and 12 bars in the second cell. The first day both logicians get the same letter from the prison master:

"The total number of bars in the two prison cells in this castle is either 18 or 20. Starting tomorrow, every morning I will go first to Ein and then to Stein, and will ask how many bars does the other logician have. If one of you answers correctly, I will immediately let both of you leave the castle. If one of you answers incorrectly, I will execute both of you. Of course, you can always decide not to answer and just stay imprisoned.
I have sent a copy of this letter both to you and your friend. There is no point in trying to communicate with him - your cells are far away from each other and he won't hear you."

Will the logicians manage to escape the castle eventually? When will they do it?


On the second day, Stein will realize that the number of bars in Ein's cell is at most 18 (assuming the windows can have 0 bars on them), because otherwise he would have guessed correctly 20. On the third day Ein will realize that the number of bars in Stein's cell is between 2 and 18, because otherwise he would have guessed the exact number. Later Stein will realize that the number of bars in Ein's cell is between 2 and 16, because otherwise Ein would have guessed the exact number. On the fourth day Ein will realize that the number of bars in Stein's cell is between 4 and 16, then Stein will realize after that the number of bars in Ein's cell is between 4 and 14. On the fifth day Ein will realize that the number of bars in Stein's cell is between 6 and 14, and Stein will realize that the number of bars in Ein's cell is between 6 and 12. On the sixth day Ein will realize that the number of bars in Stein's cell is between 8 and 12, and Stein will realize that the number of bars in Ein's cell is between 8 and 10. Then Stein will conclude that it must be 8, and that the total number of bars in the castle is 20.