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.

Wednesday, August 24, 2016

Alien Encounter

The starship Indefensible was in orbit around the planet Noncomposmentis, and Captain Quirk and Mr Crock had beamed down to the surface.
‘According to the Good Galaxy Guide, there are two species of intelligent aliens on this planet,’ said Quirk.
‘Correct, Captain – Veracitors and Gibberish. They all speak Galaxic, and they can be distinguished by how they answer questions. The Veracitors always reply truthfully, and the Gibberish always lie.’
‘But physically—’
‘—they are indistinguishable, Captain.’
Quirk heard a sound, and turned to find three aliens creeping up on them. They looked identical.
‘Welcome to Noncomposmentis,’ said one of the aliens.
‘I thank you. My name is Quirk. Now, you are . . . ’ Quirk paused. ‘No point in asking their names,’ he muttered. ‘For all we know, they’ll be wrong.’
‘That is logical, Captain,’ said Crock.
‘Because we are poor speakers of Galaxic,’ Quirk improvised, ‘I hope you will not mind if I call you Alfy, Betty and Gemma.’ As he spoke, he pointed to each of them in turn. Then he turned to Crock and whispered, ‘Not that we know what sex they are, either.’
‘They are all hermandrofemigynes,’ said Crock.
‘Whatever. Now, Alfy: to which species does Betty belong?’
‘Gibberish.’
‘Ah. Betty: do Alfy and Gemma belong to different species?’
‘No.’
‘Right . . . Talkative lot, aren’t they? Um . . . Gemma: to
which species does Betty belong?’
‘Veracitor.’
Quirk nodded knowledgeably. ‘Right, that’s settled it, then!’
‘Settled what, Captain?’
‘Which species each belongs to.’ ‘I see. And those species are—?’
‘Haven’t the foggiest idea, Crock. You’re the one who’s supposed to be logical!’
(Source: Professor Stewart's Cabinet of Mathematical Curiosities, Ian Stewart)

This is essentially a "Knights and Knaves"-style puzzle with a thin Star Trek parody veneer. Some type of constraint programming could solve these but they are fun to work out in your head. Possibly there are other ways to do it but I like to start by assuming that the first individual is of the honest "Knight" variety and seeing if the results follow.

So let's say Alfy is honest. If so, then Betty is dishonest and it is accordingly the case that Alfy and Gemma are in fact of the same species, which means that Gemma is also honest. But Gemma says that Betty is honest, which contradicts Alfy's claim that Betty is dishonest. Because these sorts of puzzles require consistency, the initial assumption that Alfy is honest must be false. Alfy is dishonest; Betty is honest and because ... [whatever pronoun for Betty is appropriate] is honest Alfy and Gemma are of the same species, making Gemma another big fat liar.

Turnip for the Books

‘It’s been a good year for turnips,’ farmer Hogswill remarked to his neighbour, Farmer Suticle.
‘Yup, that it has,’ the other replied. ‘How many did you grow?’
‘Well . . . I don’t exactly recall, but I do remember that when I took the turnips to market, I sold six-sevenths of them, plus one-seventh of a turnip, in the first hour.’
‘Must’ve been tricky cuttin’ ’em up.’
‘No, it was a whole number that I sold. I never cuts ’em.’
‘If’n you say so, Hogswill. Then what?’
‘I sold six-sevenths of what was left, plus one-seventh of a turnip, in the second hour. Then I sold six-sevenths of what was left, plus one-seventh of a turnip, in the third hour. And finally I sold six-sevenths of what was left, plus one-seventh of a turnip, in the fourth hour. Then I went home.’
‘Why?’
‘’Cos I’d sold the lot.’

How many turnips did Hogswill take to market?
(Source: Professor Stewart's Cabinet of Mathematical Curiosities, Ian Stewart)

Initially I was going to attempt to do this with a really horrible-looking single equation but there'd be no way I have enough of an attention span to hold it all together and solve it without errors. The solution I eventually realized was the right way to go, and the one the answers section suggests, is doing things recurrently, starting from the last, fourth hour. Let's state what Farmer Hogswill did in that hour algebraically:

\[\frac{6}{7}x_4 + \frac{1}{7} = x_4 \]

This states that he sold all of what remained, $x_4$, the subscript standing for the fourth hour, and that the sale was partitioned into six-sevenths of this unknown quantity and one-seventh of a turnip. The answer is one. The trick then is to take things back to the third hour. That is done accordingly:

\[ \frac{6}{7}x_3 + \frac{1}{7} + x_4 = x_3 \]

This states that the quantity of turnips Farmer Hogswill had at the beginning of the third hour was equal to six-sevenths of that quantity, plus one-seventh of a turnip as before, in addition to whatever is left behind for the next and last hour. Solving this equation, $x_3$ is equal to eight. Solving the remaining two equations:

\[ \frac{6}{7}x_2 + \frac{1}{7} + x_3 = x_2 \]

and:

\[ \frac{6}{7}x_1 + \frac{1}{7} + x_2 = x_1 \]

...it can be known that the final answer is 400.