J, 14 chars
4*-/%>:+:i.1e6
Explanation
1e6
is number 1 followed by 6 zeroes (1000000).i.y
generates the firsty
non negative numbers.+:
is a function that doubles each element in the list argument.>:
is a function that increments by one each element in the list argument.
So, the expression >:+:i.1e6
generates the first one million odd numbers:
1 3 5 7 …
%
is the reciprocal operator (numerator “1” can be omitted).-/
does an alternate sum of each element in the list argument.
So, the expression -/%>:+:i.1e6
generates the alternate sum of the reciprocals of the first one million odd numbers:
1 – 1/3 + 1/5 – 1/7 + …
4*
is multiplication by four. If you multiply by four the previous sum, you have π.
That’s it! J is a powerful language for mathematics.
Edit: since generating 9! (362880) terms for the alternate sum is sufficient to have 5 decimal digit accuracy, and since the Leibniz formula can be written also this way:
4 – 4/3 + 4/5 – 4/7 + …
…you can write a shorter, 12 chars version of the program:
-/4%>:+:i.9!