I was making a division function which takes 3 arguments: nominator, denominator and precession. The code is straight forward, but what amazed me was the thought of finding out the value of Pi. Wow, I can run this code for couple of hours by giving precession as infinite value and may get couple of million digits of Pi after decimal. Let’s run the code, and here is the output:
vishal@ubuntu:~/progs/c$ ./a.out 22 7 100
:D, WTF. I am getting repetitive pattern. I didn’t know what has happened. Talked to couple of friends and confirmed the manual division algorithm. All are getting same value. Even the calculator in Gnome (gcalctool) is giving me the same answer. This was strange. But the calculator in windows gives the actual Pi value. By doing little more googling, I found this:
I WAS SO WRONG. Till date, I was believing that 22/7 is Pi, and it is not. There’s another fractal 355/113 which is more closer to Pi than 22/7 (upto 4-5 digits :)). Here it is:
vishal@ubuntu:~/progs/c$ ./a.out 355 113 1000
Following page gives a nice idea about how Pi is approximated and what are different algorithm that modern computers are using. There are plenty of them, and one of them is invented by and Indian scientist in 1910, great, and which is the fastest I guess.
Anyway, I was wondering about the random numbers a few days ago. A friend of mine is using the /dev/random and /dev/urandom to read the random numbers. I was also using this file to read random number some time back. His application needed random numbers for encryption purpose. Suddenly the Pi thing struck into my mind that why people are not using digits of Pi as a random number. I did little bit google on this topic, and here is what I found :)
What a wonderful time spent in last couple of hour!! Life’s good.