Monday, December 12, 2016

Who Stole the Tarts? Part IV

So, who stole the pepper? Well, the King's next suspects were the March Hare, the Mad Hatter, and the Dormouse. Soldiers were sent to their house, but no pepper was found. Still, they might be hiding it somewhere, so they were arrested on general principles.

At the trial the March Hare claimed that the Hatter was innocent and the Hatter claimed that the Dormouse was innocent. The Dormouse mumbled some statement in his sleep, but it was not recorded.

As it turned out, no innocent one made a false statement and (we recall) people who steal pepper never make true statements. Also, the pepper was stolen by only one creature. Which, if any of the three, is guilty?
(Source: Alice in Puzzle Land: A Carrollian Tale for Children Under Eighty by Raymond Smullyan)

There is a kind of transitivity to this problem. Consider the first statement, made by the March Hare, that the Mad Hatter is innocent. Suppose it false. If it is false, then the Mad Hatter was the one and only thief. But because the supposition is that the March Hare lied, then the March Hare is also the thief. But this is impossible because it was stipulated that there would be only one thief. It follows thereon that the March Hare is in fact honest and that the Mad Hatter is innocent and therefore truthful. Transitively, the Dormouse is also innocent and therefore truthful. This time, none of them are guilty.

Who Stole the Tarts? Part III

"Well, here is your flour," said the King happily, "so now you can make the tarts."

"Make tarts without pepper?" asked the Queen.

"Pepper!" said the King incredulously. "You mean you use pepper in your tarts?"

"Not much," replied the Queen.

"And I suppose it was stolen!"

"Of course!" said the Queen. "Find the pepper, and when you have found out who stole it, then off with his—"

"Now, now!" said the King.

Well, the pepper had to be found, of course. Now, as you all know, people who steal pepper never tell the truth.

"What!" said Alice (not the Alice in Wonderland, but the Alice of this party). "I never heard that before!"

"You haven't?" I said in mock surprise.

"Of course not! What's more, I don't believe anybody else has either! Have any of you heard that before?"

The children all shook their heads negatively.

"Well," I said, "for purposes of this story, let's assume that people who steal pepper never tell the truth."

"All right," said Alice, a bit hesitantly.

So, to continue the story, the most obvious suspect was the Duchess's cook. At the trial she made but one statement: "I know who stole the pepper!"

Assuming that people who steal pepper always lie, is the cook guilty or
innocent?
(Source: Alice in Puzzle Land: A Carrollian Tale for Children Under Eighty by Raymond Smullyan)

This one is simple, if an assumption not explicitly included here is added. Suppose that the Duchess's cook is the thief. In addition, assume that the thief would necessarily have full knowledge of the commission of the crime. In this case, the cook would have said that she didn't know who the thief was, as pepper thieves always lie. With this assumption, we can then conclude that the cook was not lying. Of course, even outside of Wonderland, there are instances where an individual may have no memory of a heinous crime:

Taken from Phencyclidine Abuse and Crime: A Psychiatric Perspective (Bauman and Bauman)
...but including this assumption expressly might have made the answer too easy to figure out.

Who Stole the Tarts? Part II

"Now we have the jam back," said the King, "so you can make us some tarts."

"How can I make tarts without flour?" asked the Queen.

"You mean the flour was stolen?" cried the King.

"Yes!" said the Queen. "Find the miscreant, and take his head off!"
"Now, now," said the King, "let's not be hasty!"

Still, the flour had to be found. Sure enough, it was found in the home of the March Hare, the Mad Hatter, and the Dormouse, so these three were promptly arrested and tried.

At the trial, the March Hare claimed that the Hatter stole it. The Hatter and the Dormouse also made statements, but for some reason the statements were not recorded, so I cannot tell you what they were. Anyhow, as it turned out, only one of the three had stolen the flour, and he was the only one of the three who told the truth.

Who stole the flour?
(Source: Alice in Puzzle Land: A Carrollian Tale for Children Under Eighty by Raymond Smullyan)

First, note that the March Hare has somehow managed to avoid the executioner since the last episode, despite the punitive obsessions of the Queen.

But more to the point, there is only a single statement from the accused to evaluate here. That statement is: "The March Hare claimed the Mad Hatter stole it." This is necessarily false though, as "only one of the three had stolen the flour, and he was the only one of the three who told the truth". The only possible true statement of this form in this case is one of the accused saying: "I stole the flour." Accordingly, the March Hare is lying, and has not stolen the flour. Because of the content of the lie, the Mad Hatter cannot have stolen the flour either. From this we know that his testimony was dishonest, although not what it was. Given that this is a Carrollian story, it could be any number of false claims. But we can know, since neither of these other two stole the flour, that the Dormouse did, and made a true statement. Again, as this is a Carrollian story, we cannot know what the statement was. It may well not have been "I stole the flour"; it may have been irrelevant to the case entirely. But what we can infer from the sole statement made by any of the accused is that March Hare and the Mad Hatter have perjured themselves and the Dormouse is a thief.

Sunday, December 11, 2016

Who Stole the Tarts? Part I

"How about making us some nice tarts?" the King of Hearts asked the Queen of Hearts one cool summer day.

"What's the sense of making tarts without jam?" said the Queen furiously. "The jam is the best part!"

"Then use jam," said the King.

"I can't!" shouted the Queen. "My jam has been stolen!"

"Really!" said the King. "This is quite serious! Who stole it?"

"How do you expect me to know who stole it? If I knew, I would have had it back long ago and the miscreant's head in the bargain!"

Well, the King had his soldiers scout around for the missing jam, and it was found in the house of the March Hare, the Mad Hatter, and the Dormouse. All three were promptly arrested and tried.

"Now, now!" exclaimed the King at the trial. "I want to get to the bottom of this! I don't like people coming into my kitchen and stealing my jam!"

"Why not?" asked one of the guinea pigs.

"Suppress that guinea pig!" shouted the Queen. The guinea pig was promptly suppressed. (Those who have read Alice's Adventures in Wonderland will recall the meaning of the word suppress: The officers of the court put the guinea pig into a canvas bag, which tied up at the mouth with strings, and sat upon it.)

"Now then," said the King, after the commotion of suppressing the guinea pig had died down, "I want to get to the bottom of this!"

"You've already said that," remarked a second guinea pig. (This guinea pig was also promptly suppressed.)

"Did you by any chance steal the jam?" the King asked the March Hare.

"I never stole the jam!" pleaded the March Hare. (At this point all the remaining guinea pigs cheered, and were all promptly suppressed.)

"What about you?" the King roared to the Hatter, who was trembling like a leaf. "Are you by any chance the culprit?"

The Hatter was unable to utter a word; he just stood there gasping and sipping his tea.

"If he has nothing to say, that only proves his guilt," said the Queen, "so off with his head immediately!"

"No, no!" pleaded the Hatter. "One of us stole it, but it wasn't me!"

"Make a note of that!" said the King to the jury. "This evidence might turn out to be quite important!"

"And what about you?" continued the King to the Dormouse.

"What do you have to say about all this? Did the March Hare and the Hatter both tell the truth?"

"And what about you?" continued the King to the Dormouse. "What do you have to say about all this? Did the March Hare and the Hatter both tell the truth?"

"At least one of them did," replied the Dormouse, who then fell asleep for the rest of the trial.

As subsequent investigation revealed, the March Hare and the Dormouse were not both speaking the truth. Who stole the jam?
(Source: Alice in Puzzle Land: A Carrollian Tale for Children Under Eighty by Raymond Smullyan)

The book had its own, slightly different way of solving this. But here is my own:

First, recall that it is not the case that the March Hare and the Dormouse are both speaking the truth. This restricts the number of possible assignments of truth values to their statements from four to three, namely:
  • March Hare is lying; Dormouse is lying
  • March Hare is lying; Dormouse is truthful
  • March Hare is truthful; Dormouse is lying
Now, entertain, for a moment, the possibility that the Dormouse is lying in isolation. If the Dormouse is lying, then it is the case that neither the March Hare nor the Mad Hatter is truthful. If so, it is true that the March Hare stole the jam, because the March Hare said he didn't. But this would make the statement of the Mad Hatter that someone other than him of the three stole the jam true rather than false, resulting in a contradiction.

As a consequence of this, it is necessarily the case that the Dormouse is truthful. And in only one of the three options listed prior is this possible: when the March Hare is lying but the Dormouse is telling the truth. It follows immediately that the March Hare stole the jam. (Off with his head!)

Tuesday, August 30, 2016

Al-Kashi's Task

Pay for an employee work for a month (that is, thirty days) is 10 dinars and a dress. The employee worked three days only and earned the dress only. What is the price of the dress?
(Source: this terribly formatted PDF on arXiv)

Answer given in source but no justification given. What is the answer and why? Let's represent the month's salary as $10 + x$, where $x$ is the price of the dress in dinars. Let's assume that working for exactly one-tenth of the month means getting exactly one-tenth of the pay. Then the value of wages paid in cash and in kind is $\frac{10 + x}{10}$. This value is equal to the price of the dress, so $\frac{10 + x}{10} = x$, therefore $1 + \frac{x}{10} = x$ and then $1 = \frac{9}{10}x$. Accordingly, $x = \frac{10}{9}$, just a little over one dinar.

(The most valuable dinar at the time of this posting is the Kuwaiti dinar, currently worth 3.32 USD each. Admittedly naively comparing this medieval dinar to any modern one and not correcting for inflation, the monthly wages for this worker would then be under 37 USD. Those were the days, when money really had value!)

Monday, August 29, 2016

Deceptive Dice

The Terrible Twins, Innumeratus and Mathophila, were bored.
‘I know,’ said Mathophila brightly. ‘Let’s play dice!’
‘Don’t like dice.’
‘Ah, but these are special dice,’ said Mathophila, digging them out of an old chocolate box. One was red, one yellow and one blue.
Innumeratus picked up the red die. ‘There’s something funny about this one,’ he said. ‘It’s got two 3’s, two 4’s and two 8’s.’
‘They’re all like that,’ said Mathophila carelessly. ‘The yellow one has two 1’s, two 5’s and two 9’s – and the blue one has two 2’s, two 6’s and two 7’s.’
‘They look rigged to me,’ said Innumeratus, deeply suspicious.
‘No, they’re perfectly fair. Each face has an equal chance of
turning up.’
‘How do we play, anyway?’
‘We each choose a different one. We roll them simultaneously, and the highest number wins. We can play for pocket money.’ Innumeratus looked sceptical, so his sister quickly added: ‘Just to be fair, I’ll let you choose first! Then you can choose the best die!’
‘Weeelll . . . ’ said Innumeratus, hesitating.

Should he play? If not, why not?
(Source: Professor Stewart's Cabinet of Mathematical Curiosities, Ian Stewart)

This is one of those instances where averages don't really mean anything. Every one of the dice has the exact same expectation: $\frac{1}{3}(3 + 4 + 8) = \frac{1}{3}(1 + 5 + 9) = \frac{1}{3}(2 + 6 + 7) = \frac{1}{3}(15) = 5$. On this account, the game seems totally fair. So, should Innumeratus play? The names of the twins are a big hint: he should not. The way to know this is by pitting the dice against each other. For any two dice, the set of equiprobable outcomes of the game is the Cartesian product of the distinct numbers on the faces of the dice. For instance, the outcomes of casting the red die vs. the yellow die are $\{3, 4, 8\} \times \{1, 5, 9\} = \{(3, 1), (3, 5), (3, 9), (4, 1), (4, 5), (4, 9), (8, 1), (8, 5), (8, 9)\}$. In five out of these nine cases, the value of the yellow die exceeds that of the red die. Mutatis mutandis, the blue die beats the yellow die and the red die the blue die, five times out of nine. The situation described above is somewhat like playing rock-paper-scissors after you already know the opponent's choice, although more slyly, because the outcome is probabilistic rather than dead certain and, at that, nearly fair. Mathophila was attempting to lead her twin brother on with this offer to pick the "best" die, because the best die is the one chosen knowing what the other player has.

Another somewhat similar gimmick in probability that is apparently often successful in siphoning money out of people is the standard game of craps.

Sunday, August 28, 2016

Strange Counter

The following is not from a puzzle book but from the competitive programming website HackerRank. The problem in question was Strange Counter from the Algorithms section. I felt that the discussion was not adequate for help in solving the problem but I was able to write my own solution and share it with the others. Here is a full explanation. First, let's include a bit of the problem description:
Bob has a strange counter. At the first second $t = 1$, it displays the number $3$. At each subsequent second, the number displayed by the counter decrements by $1$.

The counter counts down in cycles. In the second after the counter counts down $1$, the number becomes $2 \times$ the initial number for that countdown cycle and then continues counting down from the new initial number in a new cycle. The diagram below shows the counter values for each time $t$ in the first three cycles:

In other words, first there is a countdown from $3  * 2^0$, then a countdown from $3 * 2^1$, then a countdown from $3 * 2^2$, and so it goes. If it looks like the sum of a geometric series is being used here, it absolutely is. Recall that the sum of the first $n$ terms of a geometric series is (justification here):

$$a \frac{1 - r^n}{1 - r}$$

And in this particular instance, the formula is:

$$3 \frac{1 - 2^n}{1 - 2}$$

This information is very useful for solving the problem without any kind of brute force. Given $t$, it is possible to know directly how many of these countdowns have already passed by completely. The length of these past countdowns can then be subtracted from $t$, and then $t$ can be subtracted from the length of the current countdown to find the value of the counter. Before going any further with the mathematics, consider a number of examples:
  • At $t = 1$, no counters have passed by completely. The current counter is therefore the counter that goes from three down to one. Because the counter indexing is one-based (like vectors in R, let's say), the value of the counter is $3 - t + 1$ or very simply three.
  • At $t = 2$ and $t = 3$, the same reasoning applies and the values of the counter are, respectively, two and one.
  • At $t = 4$, however, a full prior countdown has now passed. The reasoning is different here. The current countdown goes from six to one. Subtracting the length of the previous countdown from $t$ is necessary to find out what $t$ indexes relative to the current countdown. Because the length of the prior countdown was three, then the value of the counter is $6 - (t - 3) + 1$ or very simply six.
  • The same reasoning applies for $t \in \{5, 6, ... , 9\}$
  • At $t = 10$, two entire counters have passed by entirely. Their lengths, three and six, need to be added together and subtracted from $t$ so that the value of the counter can be found. Then applying the previous reasoning results in the value of 12 for the current value of the counter.
So far it seems like there are two central questions here:
  • What counter does $t$ belong to?
  • What offset needs to be subtracted from $t$ so that it can be used to index the current counter?
The answer these questions, note that $t$ is greater than or equal to the summed lengths of zero or more countdowns that have fully elapsed. This can be stated accordingly:

$$t \ge 3 \frac{1 - 2^n}{1 - 2}$$

Then:

$$
\begin{align*}
t &\ge 3 \frac{1 - 2^n}{1 - 2} \\
t &\ge \frac{3 - 3 \cdot 2^n}{1 - 2} \\
t &\ge \frac{3 - 3 \cdot 2^n}{-1} \\
t &\ge -3 + 3 \cdot 2^n \\
t + 3 &\ge 3 \cdot 2^n \\
\frac{t + 3}{3} &\ge 2^n \\
\log \left( \frac{t + 3}{3} \right) &\ge \log \left( 2^n \right) \\
\log \left( t + 3 \right) - \log \left( 3 \right) &\ge \log \left( 2^n \right) \\
\log \left( t + 3 \right) - \log \left( 3 \right) &\ge n \cdot \log \left( 2 \right) \\
\frac{\log \left( t + 3 \right) - \log \left( 3 \right)}{\log \left( 2 \right)} &\ge n
\end{align*}
$$

This means, all told, that:

$$n \le \frac{\log \left( t + 3 \right) - \log \left( 3 \right)}{\log \left( 2 \right)}$$

In programming terms, $n$, the number of fully elapsed countdowns that happened before timestep $t$, can be determined by evaluating the right-hand expression and then finding its floor (i.e. the largest integer less than or equal to it).

After doing this, the offset, namely the sum of the first $n$ terms of the geometric series can be evaluated as described above for this value of $n$ and subtracted from $t$. This value can then, in turn, be subtracted from the length of the current counter, which is $3 * 2^n$, to find the current value of the counter, with one exception: when $t$ indicates the final value of the current counter, namely, one. Here the current counter has ended, but the next has not yet begun. This case can be detected by the fact that $t$ minus the offset is equal to zero. The case can then be handled simply by printing one. In all other cases, the process I described above applies. Finally, implementing the algorithm in Scala, we have a very short and efficient program indeed:

import scala.io.StdIn
import scala.math._

object Solution extends App {
    val t = StdIn.readLine().toDouble
    // for the expression 3 * (1 - 2^n) / (1 - 2) which is the sum of the first n terms of the series
    val n = ((log(t + 3) - log(3)) / log(2)).floor
    val offset = 3 * (1 - pow(2, n)) / (1 - 2)
        
    if (t - offset == 0) {
        println(1)
    } else {
        println(f"${3 * pow(2, n) - (t - offset) + 1}%.0f")
    }
} 

Notice that, in the else clause above, the output is formatted so that the Double used for calculation will appear to be an integer. This is not a fundamental part of the algorithm but simply a requirement for the code judge to accept the output.