Best martingale for sequence of "dozen" bets at roulette game Jim goes the Casino to play roulette. He only makes 
“dozen” bets at each spin ; his probability of winning is therefore
$\frac{1}{3}$ every time (to simplify, we neglect the effect of the 
zeros in the roulette wheel), so that if his stake is $x$ units of currency, when
he wins he is returned thrice that amount, the expectation of his gain
is $0$. He has also decided to stop playing and leave the casino as soon
as he has achieved a positive gain (however small), and has brought with
him a total sum of $M$ units of currency. Finally, he decides what
amounts he bets according to a precise rule : when he has lost $k$ units
of currency ($1 \leq k &lt; M$) he bets $f(k)$ units, where $f$ is a function.
We call $f$ a martingale. Note that $f$ must satisfy 
$k+f(k)\leq M$ for every $k$ (Jim cannot bet more than he already has). 

This is a typical Markov Chain situation, with $M+2$ vertices 
$v_{+},v_0,v_1,\ldots,v_{M}$ (where $v_j$ corresponds to the point
where Jim has lost $k$ units, and $v_{+}$ means he has some positive gain). If
we denote by $\pi(k)$ the probability for Jim to eventually win from a position
where he has lost $k$ units of currency, we have

$$
\pi(k)=\frac{1}{3}\pi(k-f(k))+\frac{2}{3}\pi(k+f(k))
$$

Then, the question is, what is the best strategy (i.e. which $f$ maximizes
$\pi(0)$) ?

After looking at a few numerical examples up to $M=20$, it would seem
that there is a unique optimal $f$, satisfying $f(k)=1$ when $k$ is even
and $f(k)=\lceil \frac{k}{2} \rceil$ when $k$ is odd and $k\leq \lfloor \frac{M+1}{3}\rfloor$ (what happens for the other values of $k$ is unclear).

Does anyone know more about how to solve this exercise ?
To maximize the probability of a positive sum, you want each individual bet to give you a total net positive sum if you win.  Then your probability of winning is $1 - ({2\over 3})^N$, where $N$ is the number of bets you make.  To maximize $N$, bet the smallest amount each time that will produce winning results.

To that end, you would bet: $1$ unit on the first bet.  If you win, stop.  If you lose, then bet $1$ again, and continue with $2, 3, 4, 6, 9, 14, \ldots$ units.  The pattern is:
$$
a_i = a_{i-1} \times 1.5
$$
Round fractions down, then up, alternately.  Each $a_i$ is thus the smallest integer greater than half what you have already lost.

If you follow that betting pattern, then if you win any spin, you will end up with either $1$ or $2$ units more than you started with.

If you started with $M = 40$ units, then you can make up to 8 bets, the last being $14$.  You have $(2/3)^8 \approx 4\%$ chance of losing all $40$ units, and a $96\%$ chance of winning $1$ or $2$ units.
I don't know mathematics. I play only single dozen.  I wait for a dozen not occurred 5 times.  Then start betting on that dozen.  Progression is as follows:

4

4

8

12

20

32

52

84

130

200

310

480

I have seen maximum 15 games not occurred and lost.  But 16th game must win. You need to increase bet as above. Lot of patience required.