# How to maximize your wages?

You're trying to work out how to maximize the wages you receive in the month of February (in a non-leap year). You must work at least 5 days a week and at least 5 hours a day, but you can also work on Saturdays (not Sundays) if you wish and you may work up to 8 hours on each of Monday to Saturday. You are paid $n$ pounds (dollars, euros, whatever) per hour.

When you fill out your time-sheet at the end of the month, you know that your boss will pay you either for the number of hours you write down or, with probability $p$, for the same number of hours with the last two digits swapped. An example to demonstrate: if you work 134 hours in the month, you'll be paid $134n$ with probability $1-p$ and $143n$ with probability $p$.

How many hours should you work to maximize your expected wages? (The answer should depend on $p$.)

If that puzzle was too easy, here's a twist on it. Let's say your boss is still absent-minded enough to get the last two digits mixed up with probability $p$, but he's sharp enough to notice if the amount he pays you is more than the maximum you could possibly earn in the month. So if you work 159 hours, you'll be paid $159n$ with probability $1$ because $195n$ is too large. How does your answer change?

• Is there an upper bound to how much you can work on weekdays? What about Sunday?
– xnor
Apr 15 '15 at 9:58
• @xnor Sorry, thought I'd made that clear. I've now edited to clarify. Apr 15 '15 at 10:02
• Is your hourly rate always an integer? Can you work for factions of a hour? Can you work 5 minutes an charge for the rest of the a half hour :-)
– Bob
Apr 15 '15 at 10:03
• Bob that's lateral thinking, i don't think the OP has that in mind ;) Apr 15 '15 at 10:05
• @Bob The number of hours you put on the time-sheet has to be an integer. The hourly rate $n$ is fixed, but it doesn't matter whether or not it's an integer (if you want, just change the monetary units so $n=1$). Apr 15 '15 at 10:05

As @VincentAdvocaat said, our choice reduces to the number of hours we work within the month, where the minimum possible is $$4*5*5=100$$ and the maximum is $$4*6*8=192$$. Let's define $$X$$ is the tens digit of this number and $$Y$$ as the units digit. The average wage is therefore ($$n = 1$$):

$$W = 100 + (1 - p)(10X + Y) + p(10Y + X)$$

$$\ \ \ \ = 100 + (10 - 9p)X + (1 + 9p)Y ,$$

and this is what we want to maximise. We can think of $$X$$ and $$Y$$ as (continuous) real numbers between $$0$$ and $$9$$, and $$W$$ as a two-variable function. If it helps, you can think of $$W$$ as the height ($$z$$ coordinate) of a $$2d$$ manifold (surface) in $$3d$$. The function is linear, and therefore the surface is flat.

Since the two coefficients, $$10-9p$$ and $$1+9p$$, must be positive, the lowest point is at $$(0, 0)$$ and the surface goes up in both directions. Namely, it's always beneficial to enlarge $$X$$ or $$Y$$. So the maximum must be on a 'top-right' corner of this $$X$$-$$Y$$ plane. There are two such corners: $$(8, 9)$$ and $$(9, 2)$$.

The only question is which of them has a higher $$W$$, and this depends on $$p$$:

$$W(8,9) = 100 + 89(1 - p) + 98p = 189 + 9p$$

$$W(9,2) = 100 + 92(1 - p) + 29p = 192 - 63p$$

$$189 + 9p > 192 - 63p$$

$$72p > 3$$

$$p > 1/24$$

For $$p < 1/24$$, work $$192$$ hours. Otherwise, work $$189$$ hours. (For $$p = 1/24$$ the wage is equal, and to rand al'thor's advice I choose to work less hours...)

Second part: The change creates six special $$(X, Y)$$ values: $$(3, 9)$$, $$(4, 9)$$, $$(5, 9)$$, $$(6, 9)$$, $$(7, 9)$$ and $$(8, 9)$$, with $$W = 139, 149, 159, 169, 179$$ and $$189$$, respectively. Taking these out of our $$X$$-$$Y$$ plane, we get two more 'top-left' corners: $$(9, 2)$$ and $$(8, 8)$$.

We now compare $$W$$ for the three corners, $$(9, 2)$$, $$(2, 9)$$ and $$(8, 8)$$ with the special value $$(8, 9)$$ (the special value with highest $$W$$). The value $$(8, 8)$$ is never the best, as $$188 < 189$$. Let's check the other two corners:

$$W(9,2) = 192 - 63p > 189$$

$$63p < 3$$

$$p < 1/21$$

$$W(2,9) = 129 + 63p > 189$$

$$63p > 60$$

$$p < (1 - 1/21)$$

For $$p < 1/21$$, work $$192$$ hours. For $$p \ge (1 - 1/21)$$, work $$129$$ hours. Otherwise, work $$189$$ hours. (Again, for equal wages I choose to work less.)

• Nice and elegant, is suck at proof so i wrote a script to brute force it for p = .5 Now all i have to do is adapt the script so it can be run with different probabilities. though i suck at probabilities as well Apr 15 '15 at 13:03
• Well done! A very nitpicky nitpick: when you say $p>1/24$ and $p>1-1/21$, it should really be $\geq$ - assuming you want to work as few hours as possible within the constraint of maximising your expected wage. Apr 15 '15 at 15:22

Oke lets start with the basics, February has 28 days which are exactly 4 weeks (how convenient). You must work 5 days * 5 hours * 4 weeks = 100 hours at the very least and (assuming volunteering to work on Saturdays still gets you payed) this means 6 days * 8 hours * 4 weeks = 192 hours

So now we have the basics:

minimum 100 hours.
maximum 192 hours.


we use the following letters for our upcoming formula:

x = worked hours.
y = swapped hours.
p = probability.


I am just going to make things easy and say the money we earn is $1 for each hour. Now for the example lets state that p = 50% (to make it easy). to see what would give us the most profit we need to figure out the difference between x and y so (x - y). now since we are dealing with a 50-50 chance we can simply divide by 2 to get the average amount we would get if this is done multiple times. so (x - y)/2 what we need to do now is add the average to the worked hours. Therefor the formula is: x + ((x - y) / 2) now to get our best chance of working as little as possible and earning the most is a question of brute force, from brute force follows the best possible option is: 183 start by calculating 189 - 198 = 9 divide by 2 to get the average: 4.5 add to our start (the salary we would always get) 189 + 4.5 = 193.5 the amount of time you need to work each day (6 days) with a probability of 50% is 7 hours 50 mins As it turns out adapting this formula for a random probability value is easy: x + ((x - y) / p) simply replace the 2 by p. Through brute force we find: that after p = 24 you will earn the most with working 192 hours. which means a probability of 1/24 = 4.166....% and thanks to @Ankor we know this to be true since he has written a nice proof for it, something I'm not that good at :P For those who are interested in the c# code, keep in mind it will only work for x < 100, I'm not trying to win a beauty contest, haha: static void Main(string[] args) { for (double p = 1.0; p < 100; p++) { double highest = 0; double bestX = 0; for (double x = 0.0; x < 93; x++) { double a = Math.Floor(x / 10); double b = x - (a * 10); if (a != b) { double y = (b * 10) + a; double result = x + ((y - x) / p); if (result > highest) { highest = result; bestX = x; } } } Console.WriteLine("highest earning: " + highest + " with probability p: " + p + " for hours: " + bestX); } Console.Read(); }  • Everything down to "maximum 192 hours" is right, but the rest is a bit confused. Apr 15 '15 at 10:34 • @randal'thor i've updated it, is it better understandable now? Apr 15 '15 at 13:00 • It's better, yes :-) and I've upvoted, but Angkor's answer is even better. Apr 15 '15 at 15:23 • Yes I know I've also upvoted angkor's answer, brute forcing can give you the answer but isn't usually what is desired ;) thanks for the upvote though :) Apr 15 '15 at 17:00 When I posted this question, I didn't know the answer. Later on I worked at it for a little while and came out with a simple proof which I think is nice enough to post as a self-answer. Before you read it, though, go and upvote Angkor's answer. The multiplier$n$is irrelevant, so WLOG we can assume$n=1$. By working for 189 hours, you can ensure a minimum possible wage of$189$and therefore an expected wage of at least$189$. The only way you could possibly earn more than this is by working for 190, 191, 192, 109, 119, or 129 hours. So one of these seven numbers must be optimal. The expected wage in each case is: • 189 hours:$198p+189(1-p)=189+9p$in first problem,$189$in second problem • 190 hours:$109p+190(1-p)=190-81p<192-63p$• 191 hours:$119p+191(1-p)=191-72p<192-63p$• 192 hours:$129p+192(1-p)=192-63p$• 109 hours:$190p+109(1-p)=109+81p<129+63p$• 119 hours:$191p+119(1-p)=119+72p<129+63p$• 129 hours:$192p+129(1-p)=129+63p<189+9p$In the first problem, you have a choice between working for 189 hours and earning$189+9p$or working for 192 hours and earning$192-63p$. Some simple manipulation of linear inequalities shows that$189+9p<192-63p\Leftrightarrow p<\frac{1}{24}$. So you should work for 192 hours if$p<\frac{1}{24}$and 189 hours otherwise. In the second problem, you have a choice between working for 189 hours and earning$189$, working for 192 hours and earning$192-63p$, or working for 129 hours and earning$129+63p$. Some simple manipulation of linear inequalities shows that$189<192-63p\Leftrightarrow p<\frac{1}{21}$and$189<129+63p\Leftrightarrow p>\frac{20}{21}$. So you should work for 192 hours if$p<\frac{1}{21}$, 129 hours if$p>\frac{20}{21}\$, and 189 hours otherwise.

Tried to brute force the scenario and got

189 hours is the most efficient answer with an average of 94.5 across all values of p with precision up to 2 decimals. PHP program can be found HERE

Considering you are getting paid on Saturdays too.

• If p=0.0001 wouldn't 192 be better? Apr 15 '15 at 12:33
• @leoll2 ;) no doubt, but you need to consider each value of p in the sample space for a given value of working hours and then take average. That's what i have tried. and for unknown value pf p 189 is optimal. Apr 15 '15 at 12:37
• "The answer should depend on p." It's written in the OP. Apr 15 '15 at 14:43
• leoll2 is right. 189 is optimal only when p is at least 1/24 (in the first problem). See Angkor's answer or my own self-answer for a proof. Apr 15 '15 at 19:40

After a catastrophic misreading of the question I wrote an answer all about how much different values of n affect the outcome. Needless to say they don't, so here as compensation for anyone who wasted their time reading it, is a graphical representation of the possible amounts of pay. I have my doubts about exactly how accurate it is, but if a picture is worth a thousand words, even if some are wrong it's closer than what I originally wrote.

• It seems to me that in the question the digit switching happens on the number of hours, not the amount of the pay. It should be irrelevant what is the unit. By the way, there is little I wouldn't do for 10 gold bars an hour. Apr 15 '15 at 13:03
• Yeah I noticed my mistake when I started reading other peoples answers. Couldn't quite put my finger on why the made perfect sense but bore no relation to mine. Then the penny dropped why rand al'thor didn't seem to car what the value of n was. Any way I'm replacing my ramblings with a nice picture.
– Bob
Apr 15 '15 at 13:34