Post

A Probability Problem Inspired by Choosing a Phone Number

Update: Tragedy — lost a blog post I’d been writing for over an hour. Safari is garbage. I didn’t press F5, so why did it auto-refresh? I’d written for ages, then poof, everything gone. Then WordPress’s auto-save draft kicked in, and the draft turned into a blank post. Lesson learned: online editors are not reliable.


Today I went to buy a Hong Kong phone number. The streets are full of Peoples, which is just China Mobile under a different brand. Other carriers require you to have your ID card before you can sign up. Fortunately, number portability is easy here. The annoying thing about HK phone numbers is that you only see the number after you’ve paid and activated — unlike in China, where “golden numbers” are a whole industry. My first number for 60 HKD was 54872341 (for Chinese speakers, the digits sound quite ominous). I’m not superstitious myself, but I can’t stop the people who contact me from being superstitious. So I spent another 60 HKD and bought another card. It still had a 4, but better than the first one — at least it doesn’t invite morbid associations. This got me thinking: what’s the probability of getting an 8-digit number that contains the digit 4?

I wrote a program first to get the empirical probability:

bool hasFour(int n) { while (n > 0) { if (n % 10 == 4) return true; n /= 10; } return false; } int main (int argc, const char * argv[]) { int cnt = 0; ::srand(::time(0)); for (int i = 0; i < 100000000; i++) { // sampling this many times int n = rand()%100000000; // randomly generate an 8-digit number if (hasFour(n)) // check if it has a 4 cnt++; } printf("%d\n", cnt); return 0; }

Result: about 57%.

Still not satisfied. After some agonizing calculation, I found the general formula:

Where n is the number of digits in the phone number. This was generated with an online LaTeX editor — looks great.

Then I revised the program:

int Fractal(int n) // classic recursive factorial { if (n == 1) return 1; else return Fractal(n 1) * n; } int C(int u, int d) // calculate combination { return Fractal(u)/(Fractal(d) * Fractal(u d)); } int GetDigt(int n) // calculate numerator { int ret = n; for (int i = 1; i <= n 1; i++) { ret += ::pow(9, i) * C(n, i); } return ret; } int main (int argc, const char * argv[]) { printf("%f\n", GetDigt(8)/::pow(10, 8)); return 0; }

This time I got the exact probability: 0.569533

Buying another card still comes with a high risk of getting a 4. So I gave up and settled on the second number, which starts with 6641. Not bad — the number even contains some sensitive keywords. At least I won’t get spam texts from web crawlers inside the Great Firewall.

This post is licensed under CC BY 4.0 by the author.