18
votes
\$\begingroup\$

This comes from http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/

"Given that Pi can be estimated using the function \$4 \times (1 - 1/3 + 1/5 - 1/7 + \cdots)\$ with more terms giving greater accuracy, write a function that calculates Pi to an accuracy of 5 decimal places."

  • Note, the estimation must be done by calculating the sequence given above.
\$\endgroup\$
10
  • 10
    \$\begingroup\$ You should probably add some more rules, otherwise you will get answers like (python) p=lambda:3.14159 \$\endgroup\$
    – Matt
    Commented Aug 29, 2012 at 15:44
  • 1
    \$\begingroup\$ Have you seen codegolf.stackexchange.com/questions/506/… , which is very similar? At the very least, trig functions should be banned for this problem because they allow for trivial solutions such as this QBASIC program: ?INT(4E5*ATN(1))/1E5 \$\endgroup\$ Commented Aug 29, 2012 at 17:30
  • \$\begingroup\$ I think you should require that the algorithm be one of successive approximation: the longer you compute, the closer you get to pi. \$\endgroup\$
    – DavidC
    Commented Aug 29, 2012 at 22:59
  • 2
    \$\begingroup\$ Dupe, but it's so old it's not here: stackoverflow.com/q/407518/12274 \$\endgroup\$
    – J B
    Commented Sep 1, 2012 at 21:42
  • 4
    \$\begingroup\$ I’m voting to close this question because use of a specific algorithm is impossible to objectively validate \$\endgroup\$
    – ATaco
    Commented Jul 20, 2023 at 3:51

31 Answers 31

1
2
0
votes
\$\begingroup\$

CJam, 20 bytes

V4e5{4T2*)d/WT#*+}fT

Try it online!

One byte shorter than aditsu's answer. ;)

Alternate 20-byter

4_e5,f{_2*)W@#*d/}:+

Try it online!

Does the same thing, but I like this better for some reason.

\$\endgroup\$
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.