The whole and half pill problem

November 15th, 2007

The idea for this problem came to me while taking some vitamin pills. Each day I take half a pill. If I draw a whole pill from the container, I cut it in half, take one half and put the other half back in the jar, if it’s a half, I take it. Very simple… Then I asked myself what the average distribution of whole/half pills would be after i days, assuming that the probability of drawing a specific whole or half is P = 1/k where k is the number of pills (wholes and halfs) remaining (which is not rally the case for a jar - this problem can be generalized for different probability functions). So here is my reasoning.Assuming that on day i I have m(i) wholes and n(i) halves, and that m(i) + n(i) = S(i), than the probability to draw a whole is

P(m(i)) = m(i)/S(i),

and the probability to draw a half is

P(n(i)) = n(i)/S(i)

So on average, after day i I have

m(i+1) = m(i) - P(m(i)),

and

n(i+1) = n(i) + P(m(i)) - P(n(i))

because n(i) will increase if I draw a whole, and decrease if I draw a half.

So the equations are:

initial values m(0) = P, n(0) = 0

m(i+1) = m(i) - m(i)/S(i)

n(i+1) = n(i) + (m(i)-n(i))/S(i)

I simulated this in two ways in a small program, once using these set of equations and setting P = 15000, and also with a simulation of the real process using random numbers, and they both yielded the  same results and graphs

formula1.jpgformula.gif

The question is, how do you turn this set of recursive equations into a set of 2 non-recursive functions to represent each m(i) and n(i) as functions of i and initial conditions. Or at least, how could you determine some noticeable points, such as the point of intersection (when m(i) equals or crosses n(i)), or maximum of n(i) or the function for the sum or difference or n(i)/m(i). I have solved similar problems but the equations were somewhat simpler, and they could be reduced easily using a smart grouping of terms. I was also wondering if this could not be solved using calculus, by turning the recursive equations into functions of a continuous variable i and saying that m(i+i)-m(i) = dm (differential of m) and integrating them somehow, but the last time I integrated differential equations was in college, years ago.

From the chart, they don’t look like simple functions, although I may be wrong. The problem could be generalized for different probability functions and/or for different fractions of a pill per day.

XML Content Model Validator project

October 23rd, 2007

I just made public an older project of mine which I developed while working for a hot Internet company (go here to see the documentation and download the source and binary code). The company went under a few years ago, but this doesn’t mean the code must go under too…

This was a particularly challenging task for several reasons.

Technically, I had to replace a messy chunk of code which simply couldn’t handle the task and wasn’t easily extensible or maintainable. Also, the problem itself wasn’t simple, and I couldn’t find an immediate and intuitive solution, so I had to do some more fundamental research.

At the same time, things were kind of tense in the team back then, and that didn’t help either…

Nevertheless, this project ended successfully and I believe contributed to the robustness of the rest of the code. 

I also learned a great deal about these subjects from a completely different perspective. For example, I was quite familiar with using state machines in hardware or software, but building a state machine from a regular expression was something quite mysterious to me up to then.

All in all, I enjoyed working on it back then, and I enjoyed un-mothballing and publishing it now.