There's a Python library that's usable at the command line, too: https://github.com/joergdietrich/daltonize.
That project led directly to the 'rand' call in this function  13 years later and fundamentally altered the way I think about data analysis.
Someone here suggested the Steinmetz solid as a follow up which I will try next time I teach that group.
I didn't get it at the time and I deeply resented the problem. What does this have to do with being a software engineer?
But interesting to see how it's actually done.
edit: even within those fields, it's definitely more of a very-specific-domain-knowledge type of question and not something that I think is worth the time to ask it in a job interview.
1. You can do one quadrant rather than four, then multiply that approximation by 4.
2. Montecarlo is good to develop an intuition about Monte carlo approximations but converges really slowly.
Ramanujan-Sato series for pi or Chudnovsky algorithm can be a scalable alternative, gives you a lot of digits.
Now... how many digits you usually need? Very few. Practical applications requiring more than 15 digits are hard to find. e.g: NASA uses 15 digits for interplanetary navigation, even for the most distant spacecraft such as the Voyager 1 which is now 20 billion kilometers from Earth.
Can just use a constant, or in the worst case, if missing, define it as:
pi = arc tan (1) * 4
To get better accuracy you would need to cut out some parts that are clearly inside or outside the circle, and only throw the points in the areas where the circle can actually be.
from random import random
l = lambda (a, b), c: (a + c, b + 1)
g = (1 if random()**2 + random()**2 <= 1 else 0 for i in range(10**6))
r, t = reduce(l, g, (0.0, 0))
print r / t * 4
python3 -c 'from random import random; N=1000000;print(4*sum([ int(random()**2 + random()**2 <= 1.0) for i in range(N)])/N)'
|>Seq.sumBy(function _ when d()+d()<0.25->4.0/n|_->0.0)
sum(runif(10^6, 0, 1)^2 + runif(10^6, 0, 1)^2 < 1) / 10^6 * 4
You can click on it, sign in to run, edit, etc.
Sign in: any outlook, xbox, hotmail, or msft ID.