20

This is a fun puzzle I was assigned on the first day of highschool (over a decade ago). I just dug it up randomly from under my bed and thought I'd share it with the SE community. At the time, I found a better solution than even the Puzzle book had, and nobody was able to come up with a better answer...

A farmer has 3000 bananas that will be sold at a supermarket located 1000 kilometers away. To get them there, he has a camel that is strong enough to carry 1000 bananas at a time, but will eat 1 banana for each kilometer it walks. Will the camel successfully deliver any bananas to the supermarket? If yes, how many, and how?

Have fun! :D (I will reveal my personal 9th grader solution if no-one finds it)

Qiaochu Yuan
  • 419,620
Anson Kao
  • 311

2 Answers2

22

Here is the optimal solution for an arbitrary capacity $c$, amount $n$ and distance $x$: $ \def\lfrac#1#2{{\large\frac{#1}{#2}}} $

First note that the path of the camel can be divided into segments determined by all the turning points. Then each segment is traversed an odd number of times. We can assume that there is none left at any point because otherwise we can simply consume any leftover amount $r$ by moving forward and backwards a number of times over appropriate distances to consume it (the appropriate distance is $\frac12r$ if $r≤c$, otherwise it is $\frac12c$). Also the camel never needs to traverse a former segment once it has traversed a later segment, because otherwise it could have traversed the former segment first since there would have been a sufficient supply already brought to that point earlier. (See below for proper proof.) Thus we need only consider solutions in which that the camel brings everything from the current point to the next point in one or more trips (and never returns to an earlier point).

Now consider the state of the camel between such phases of a given solution as captured by a point in the $(n,x)$-plane. That is, each segment in the path corresponds to moving from a point $(n,x)$ to another point $(n',x')$ where there are initially $n$ bananas at a single turning point and the camel was initially at that point with remaining distance $x$, and after that segment there are $n'$ bananas at the next turning point and the camel has reached that point with remaining distance $x'$.

Observe that the line segment from $(n,x)$ to $(n',x')$ has gradient $\lfrac{ 1 }{ 2 \lceil \lfrac{n}{c} \rceil - 1 }$, since the camel must traverse that segment $( 2 \lceil \lfrac{n}{c} \rceil - 1 )$ times so as to leave no bananas behind, which would consume $( 2 \lceil \lfrac{n}{c} \rceil - 1 ) · (x-x')$ bananas. Notice that this gradient is bigger when $\lceil \lfrac{n}{c} \rceil$ is smaller, and that a bigger gradient is better because the camel covers more distance per banana. So it is clearly best if we start a new line segment whenever $\lceil \lfrac{n}{c} \rceil$ decreases, which is precisely whenever $n$ is a multiple of $c$. This yields a unique solution, which must hence be optimal. (I shall leave it as an exercise to rigorously prove that any other solution is no better than this; one of the easiest ways is to use induction on $\lceil \lfrac{n}{c} \rceil$.)

Let $f(c,n,x)$ be the maximum amount that can be brought a distance of $x$ where an amount $n$ and the camel with capacity $c$ are at the starting point. Then we have:

$ f(c,n,x) = \cases{ 0 & if $n < x$ \\ n & if $x = 0$ \\ n - ( 2 \lceil \lfrac{n}{c} \rceil - 1 ) · x & if $( 2 \lceil \lfrac{n}{c} \rceil - 1 ) · x ≤ r$ \\ f( c , n - r , x - \lfrac{ r }{ 2 \lceil \lfrac{n}{c} \rceil - 1 } ) & otherwise }$
  where $r = n + c - c \lceil \frac{n}{c} \rceil$ (i.e. $r$ is the least positive real such that $n-r$ is a multiple of $c$).

The first case excludes impossible situations. The second case is when the camel has reached the destination. The third case is when using one phase (i.e. one line segment) to reach the destination is optimal, in contrast with the fourth case where attempting to reach the destination in a single phase would cause the bananas at the destination to be less than the nearest multiple of $c$ below $n$, which by the earlier reasoning is not optimal. So the fourth case is recursive because we want the camel to consume only $r$ bananas to shift the rest in one phase and then begin another phase after that.

For this example:
$f(1000,3000,1000)$
$ = f(1000,2000,\frac{4}{5}(1000))$
$ = f(1000,1000,\frac{7}{15}(1000))$
$ = \frac{8}{15}(1000) = 533\frac{1}{3}$

Interestingly, the maximum distance that the camel can go with a starting heap of $cn$ bananas is $c ( \frac{1}{2} \ln(n) + O(1) )$, so an exponential number of bananas is needed with respect to the distance from the starting point!

−−−−−−−

From the comments it has become clear that it is difficult for some people to find a proof of the above claim that the camel never needs to traverse a former segment after it has traversed a later segment. Indeed, this is a rather tricky exercise; most naive proof attempts will fail completely. Here is a proper proof:

Take any path $P$ that performs finitely many turnings. Divide $P$ into segments according to all the turning points (i.e. each segment is from some turning point to some adjacent turning point). Then according to $P$, whenever the camel is at the starting point or a turning point and has not finished, it must go to an adjacent turning point. For convenience, for any turning points $A,B$ we shall say that $A < B$ iff $A$ is closer than $B$ to the starting point. I claim that there is some path $Q$ that reaches the same final point with the same number of bananas there as $P$ that does not go backwards twice, meaning that for every consecutive turning points $A<B<C$ in $Q$, once the camel reaches $C$ it never goes back to $A$. (Note that turning points only include points where the camel actually turned.)

To construct $Q$, we shall iteratively modify $P$ over a finite sequence of steps until it is as desired. Until then, at each step the current $P$ has some backward chain of length at least two, meaning that it has a chain of at least two consecutive backward moves. Let $C→B→A$ be the last two moves of the first backward chain of length at least two. (This "last" and "first" are crucial; removing them makes the proof totally fall apart.) Then $P$ has a subpath $A→B→C→$ $\cdots$ $→\underline{C→B→A}→B$ where the marked (underlined) part is the last two moves of the first backward chain of length at least two. It must be "$A→B$" after that part because otherwise the marked part would not be "the last two moves" in that backward chain. And every turning point in the "$\cdots$" is further than $A$ from the starting point, because otherwise the marked part would not be the "first backward chain of length at least two". Now modify that subpath to $A→B→A→B→C→$ $\cdots$ $→C→B$ (keeping the "$\cdots$" the same). The new subpath consumes the same bananas, so we just need to modify the way the camel carries the bananas in that subpath in such a way that the number of bananas left at each of the points involved is the same at the end of the subpath. All the bananas consumed in the shifted moves $B→A→B$ were carried through the first $B$ in the original subpath, because before that subpath the camel did not reach $C$ (otherwise the marked path would not be the "first backward chain of length at least two"), and so there were no bananas at $C$ or further. Thus in the original $P$ we could have more efficiently left those bananas at that first $B$ instead of carrying them around. In the new subpath, follow that more efficient carrying procedure, so we can clearly consume those bananas in the shifted moves $B→A→B$ since we do not need them later.

We need to prove that this iterative process terminates! One way is to count the number of inversions in the current path $P$, namely the number of pairs of positions $i<j$ such that the $i$-th point in $P$ is further than the $j$-th point in $P$ from the starting point. At each step, every point in the "$\cdots$" is at $B$ or further (as proven above), so the shift of the points $A,B$ across that does not change the number of inversions that involve positions inside the "$\cdots$". But the number of inversions involving the $A,B$ and positions in the subpath outside the "$\cdots$" does decrease by at least four due to shifting across the two $C$s. Therefore the total number of inversions decreases at each step. Since the initial path $P$ had a finite number of inversions, and the number of inversions clearly cannot go below zero, the iterative process must stop after finitely many steps.

Thus we have proven the claim, which implies that when restricted to paths with finitely many turnings, it suffices to find an optimal path among only the paths that do not go backwards twice. And then the rest of the argument in the original post proceeds.

Finally, I shall leave here an even harder exercise for the more advanced reader: The above proof only constructs a path that is optimal with respect to paths that perform finitely many turnings. Prove that this path is in fact optimal with respect to all continuous paths (on the line) with constant speed, even for those with infinitely many turnings! The reason I did not consider this issue in my original post was that in reality you cannot make infinitely many turnings.

user21820
  • 57,693
  • 9
  • 98
  • 256
  • What if he travels to an earlier point and the following possibility arises: 1) the total of the supply at that point and the amount he's carrying when he reaches that point, is enough to allow him to traverse the segment but 2) the supply at that point itself is not enough to allow him to traverse that segment. Then your argument that "he could have traversed the former segment first" would not apply. – Siddharth Joshi Jun 14 '20 at 08:27
  • 1
    @SiddharthJoshi: No. Given 3 adjacent stopping points A,B,C, the claim is that if you go A→B→C then you never need to go back to A. It is not a trivial argument, but it is not hard. Suppose there is a path that goes backwards twice C→B→A. Then we can assume C→B→A is the last two steps in the first backward chain of length at least 2. So the path must have A→B→C→...→B→A→B. Now we simply need to check that we can change it to A→B→A→B→C→...→B, since prior to this subpath we could not have reached C (otherwise there would be an earlier backward chain of length at least 2). – user21820 Jun 14 '20 at 09:07
  • Is there a rigorous proof you have for this. I think you would also need to prove that the amount finally remaining at each of those positions in that sequence after the exchange operation, is greater than or equal to their amounts for the previous sequence. Can't really extend your argument to a rigorous proof - I will have to construct a more formal proof for myself on my own. – Siddharth Joshi Jul 03 '20 at 19:16
  • @SiddharthJoshi: Yes I commented because I believed I could write a rigorous proof but was lazy to. The change to that subpath preserves the total distance travelled and hence we just have to ensure that we can leave behind the same amount at each point at the end of the modified subpath. By my definition, the "..." stays at C or later, and then the "B→A→B" uses 2·dist(A,B) bananas that we had carried from before C (since prior to that subpath there are no bananas at C or later. Label those bananas and retrace our steps in the original subpath and instead leave them behind at B. [cont] – user21820 Jul 04 '20 at 09:33
  • [cont] Now move that "B→A→B" earlier to obtain the modified subpath, using up those labelled bananas first. It will not affect the other part of the subpath since we never used those labelled bananas there. Are you able to complete the proof now? – user21820 Jul 04 '20 at 09:35
  • Thanks for the extra effort. I figured out a simpler proof for me some time after posting my earlier comment. Consider the sequence of points visited by the camel from the beginning and consider that there is at least one earlier point. Then consider the first earlier point ( i.e. a point that is visited back from a non-adjacent point ahead of it ), call it A. Then the sequence looks like ... -> A -> [ B -> C ] -> B -> A -> B ( $\cdot$ [] means repetition $\cdot$ The point C is never visited before it's first visited in the sequence above ) [contd] – Siddharth Joshi Jul 04 '20 at 10:02
  • [contd] Then the total amount ( = on his back + at current point ) at B after [] must be less than or equal to the total amount at B at starting of []. This is again true because of what you said - C doesn't have anything initially first. Say he carries back "y" bananas from B->A after [], then since the total amount at B at starting of [] is greater than the amount at B after [], he should be able to carry "y" bananas then itself. After reaching A, he carries "z" bananas what he would have done in the last step in the sequence above, and the amount at A remains unchanged by this chng [contd] – Siddharth Joshi Jul 04 '20 at 10:08
  • [contd] Also since the total amount at B after bringing $z-dist(A,B)$ bananas $= amount(B \ @ \ end \ of \ []) + (z-dist(A, B))-y+amount(C)+2trips(B, C)dist(B, C)$ and since $amount(B \ @ \ end \ of \ [])-y>=0$, it is possible to carry out the rest of traversals in [], such that the final amounts at both B and C are equal. – Siddharth Joshi Jul 04 '20 at 10:18
  • 1
    @SiddharthJoshi: Your attempt seems to fail. Your "earlier point" is no different from my "backwards twice". But if the path is A→B→C→D→C→B→A, your first earlier point is B, and so your claim that it must go B→C from that point is false. That is precisely why I said "we can assume C→B→A is the last two steps in the first backward chain of length at least 2". It's not as simple as you think. I suggest you try to work carefully through the sketch I gave. If you have trouble, we can continue in chat. – user21820 Jul 04 '20 at 10:21
  • "Then the number of times each same segment is crossed is even except for the first and last segment." Can you please explain why this is ? – Hemant Agarwal Jan 24 '21 at 12:52
  • @HemantAgarwal: I have amended the phrasing (the original was written so long ago that I no longer recall why exactly I wrote it), and also added a proper proof of the key claim. – user21820 Jan 24 '21 at 15:34
  • It is clear to me why, when a new segment starts, then a previous segment no longer should be visited. I also understood why f(c,n,x)=max({f(n−(2⌈nc⌉−1)y,x−y):0<y≤x}) for all x>0 . But I am unable to understand any of the calculations after this equation . Can you please explain in simpler language, the calculations after the above equation ? Thanks – Hemant Agarwal Jan 24 '21 at 21:27
  • 1
    @HemantAgarwal: I rewrote that part too. I think it should be much better than the original! (After all, it's been 9 years!) =) – user21820 Jan 25 '21 at 09:44
  • You gave this equation: (n-n')/(x-x') = (2⌈n/c⌉−1) . Let's try to find the first turning point of the case where there are 3000 bananas at the start and the camel has a capacity of 1000. Therefore (2⌈n/c⌉−1) = (2⌈3000/1000⌉−1) which is a constant . Therefore , whether we make the first turning point as 100 Kms or 200 Kms or 400 kms, the cost ( bananas consumed per km ) of getting the bananas to the first turning point will be the same in each case. Then how do we figure out where the first turning point should be ? ( I have read at other places that the first turning point is at 200 Kms) – Hemant Agarwal Jan 26 '21 at 04:44
  • @HemantAgarwal: I explained that in: "So it is clearly best if we start a new line segment whenever $⌈n/c⌉$ decreases". Although banana/km is constant for making a fixed (odd) number of trips over a segment, if we make the distance too far then we are making too many trips. That is why you need to compute how long the first segment should be so that after traversing it using $(2⌈n/c⌉−1)$ trips you end up with a multiple of $n$ bananas (unless you run out of bananas), because then you need less trips on the next segment. That is precisely why you need to compute $r$. – user21820 Jan 26 '21 at 05:56
  • It's best if you actually draw out the situation in the $(n,x)$-plane. Moving the first turning point further literally lengthens the first line segment without changing the gradient. But you can do better by stopping the first line segment exactly when $⌈n/c⌉$ is an integer. Plot the alternative route and you will see! =) – user21820 Jan 26 '21 at 05:59
6

Ok - it is an answer, that I think is optimal. When I first made this comment, I was unsure. But 533 seems to be the answer.

First go 200 kilometers, drop off 600, and turn around. Repeat. So now we have 2000 bananas, 200 kilometers away from where we began.

Now travel 333 and a third of a kilometer, drop off 333 and a third, and return to the 'wait station' 200 kilometers away from where we began. There are 1000 bananas left, and so we go in one run to the end. We do, of course, pick up the 333 and a third bananas that we left (conveniently exactly 333 and a third kilometers away).

So we end with 1000 - 800 + 333 and a third, or 533 and a third bananas. And I think this is optimal.

  • So I guess I shouldn't be surprised that the collective hive-mind of the internet would be able to find my 9th grade solution - mixedmath, you nailed it. The shocking thing is that the solution in the puzzle book had the stops at 250 km and 500 km, yielding 500 km as the answer, and nobody else including the teacher knew any better! – Anson Kao Jul 26 '11 at 04:09
  • 1
    @SampleJACK: Did you have a good method for coming across this answer? Mine was somewhat obscure. – davidlowryduda Jul 26 '11 at 04:15
  • 1
    the most important observation in solving this problem is that the stopping points should be optimized to a multiple of 1000 bananas, since that is the upper capacity of the camel and optimal. From that, I just fiddled with numbers I guess. – Anson Kao Jul 28 '11 at 16:28
  • @SampleJACK: yeah, I did that too. Lots of fiddling. – davidlowryduda Jul 28 '11 at 16:47
  • Just to let you know, I just updated my answer with a proper proof of a key claim. It wasn't easy to find a proof! =) – user21820 Jan 24 '21 at 15:48