Saturday, February 12, 2011

My first Python program

I heard of Python in my final year of B.Tech. at CU. Since then I have tried many times to pick up the language. Unfortunately most of the efforts have not gone far mostly due to lack of a particular problem to attack.

This time however it went differently. First I came across this brilliant and simple way (Monte Carlo technique for computing Pi) to compute the value of Pi from some CS magazine that I ran across in Dept of CSA [1]. Then I wanted to implement it. Matlab and C would have been the perfect choice for writing this simple code, but I just had enough time for trying it out in Python.

Here is the result of that (after 50mins involving mostly Google and the Python website):


Maybe I could have improved the code a lot had I been an "evolved Python programmer". But I am pretty happy with the initial results that I got. The great thing is that with iteration count 100K or so the program almost always computes upto 3.14 correctly.

[1] Maybe Communications of the ACM, some issue of 2010, however the link given before also explains the same technique

No comments:

Post a Comment

I'd love to hear from you !