Friday, 4 March 2011

Approximating Pi using a Monte Carlo algorithm in Q

Probabilistic data structures and algorithms have unique special properties that complement distributed systems and mathematical modelling. I will be writing more blog posts to elaborate on these properties and their applications for high performance computing.

I decided to write a function in Q that uses monte carlo simulation to approximate Pi as Q is very concise and highly optimized for performing vector algebra. The result was just one line of code which is blazing fast.

pi:{[n]x:n?1.0;y:n?1.0;4*(count where 1>=sqrt (x*x)+y*y)%n}

Voila! A Pi approximation in one line of Q.

No comments:

Post a Comment