Skip to content

Instantly share code, notes, and snippets.

@ShiangYong
Created December 30, 2016 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShiangYong/f85a481510fdb860da3d13f54b044c0e to your computer and use it in GitHub Desktop.
Save ShiangYong/f85a481510fdb860da3d13f54b044c0e to your computer and use it in GitHub Desktop.
Mathematica module to solve the Stormtroopers puzzle for general N>9, http://fivethirtyeight.com/features/build-your-own-death-star-and-defeat-the-stormtroopers/
calcLimitingProb[K_] :=
Module[{p, n, plose, pdrop, pwin, tmp, solution},
p = {(999 K)/(1000 + 999 K)};
solution = {N[1000/999]};
For[n = 2, n <= 20, n++,
pdrop = (K*Sqrt[n]/1000)*(999/1000)^n;
plose = 1 - (999/1000)^n + (1 - p[[n - 1]])*pdrop;
pwin = p[[n - 1]]*pdrop;
tmp = Simplify[pwin/(plose + pwin)];
AppendTo[solution, K /. NSolve[tmp == 0.5 && K > 0.0, K][[1, 1]]];
AppendTo[p, tmp];
];
Return[solution];
];
@ShiangYong
Copy link
Author

Here are the solutions from N=1 to N=10:
{1.001, 2.90532, 5.29962, 8.09774, 11.2488, 14.7178, 18.4785, 22.5104, 26.7969, 31.3241}

and from N=11 to N=20:
{36.0804, 41.0557, 46.2411, 51.6288, 57.2119, 62.9841, 68.9399, 75.0741, 81.3819, 87.8591}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment