J, 73 64 62 bytes
(j.":"+10&^(<.@*%[)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8)@i.@>:
This outputs each approximation to n digits as a formatted string. This uses the polynomial simplification of the formula and gets the first n digits by multiplying the sum by a power of 10, flooring it, and dividing by that same power of 10.
The input is taken as an extended integer, meaning that rationals are used when division occurs which keeps results exact.
Usage
This is the output for n = 100, showing the cumulative sums for k in [0, 100].
f =: (j.":"+10&^(<.@*%[)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8)@i.@>:
f 100x
3
3.1
3.14
3.141
3.1415
3.14159
3.141592
3.1415926
3.14159265
3.141592653
3.1415926535
3.14159265358
3.141592653589
3.1415926535897
3.14159265358979
3.141592653589793
3.1415926535897932
3.14159265358979323
3.141592653589793238
3.1415926535897932384
3.14159265358979323846
3.141592653589793238462
3.1415926535897932384626
3.14159265358979323846264
3.141592653589793238462643
3.1415926535897932384626433
3.14159265358979323846264338
3.141592653589793238462643383
3.1415926535897932384626433832
3.14159265358979323846264338327
3.141592653589793238462643383279
3.1415926535897932384626433832795
3.14159265358979323846264338327950
3.141592653589793238462643383279502
3.1415926535897932384626433832795028
3.14159265358979323846264338327950288
3.141592653589793238462643383279502884
3.1415926535897932384626433832795028841
3.14159265358979323846264338327950288419
3.141592653589793238462643383279502884197
3.1415926535897932384626433832795028841971
3.14159265358979323846264338327950288419716
3.141592653589793238462643383279502884197169
3.1415926535897932384626433832795028841971693
3.14159265358979323846264338327950288419716939
3.141592653589793238462643383279502884197169399
3.1415926535897932384626433832795028841971693993
3.14159265358979323846264338327950288419716939937
3.141592653589793238462643383279502884197169399375
3.1415926535897932384626433832795028841971693993751
3.14159265358979323846264338327950288419716939937510
3.141592653589793238462643383279502884197169399375105
3.1415926535897932384626433832795028841971693993751058
3.14159265358979323846264338327950288419716939937510582
3.141592653589793238462643383279502884197169399375105820
3.1415926535897932384626433832795028841971693993751058209
3.14159265358979323846264338327950288419716939937510582097
3.141592653589793238462643383279502884197169399375105820974
3.1415926535897932384626433832795028841971693993751058209749
3.14159265358979323846264338327950288419716939937510582097494
3.141592653589793238462643383279502884197169399375105820974944
3.1415926535897932384626433832795028841971693993751058209749445
3.14159265358979323846264338327950288419716939937510582097494459
3.141592653589793238462643383279502884197169399375105820974944592
3.1415926535897932384626433832795028841971693993751058209749445923
3.14159265358979323846264338327950288419716939937510582097494459230
3.141592653589793238462643383279502884197169399375105820974944592307
3.1415926535897932384626433832795028841971693993751058209749445923078
3.14159265358979323846264338327950288419716939937510582097494459230781
3.141592653589793238462643383279502884197169399375105820974944592307816
3.1415926535897932384626433832795028841971693993751058209749445923078164
3.14159265358979323846264338327950288419716939937510582097494459230781640
3.141592653589793238462643383279502884197169399375105820974944592307816406
3.1415926535897932384626433832795028841971693993751058209749445923078164062
3.14159265358979323846264338327950288419716939937510582097494459230781640628
3.141592653589793238462643383279502884197169399375105820974944592307816406286
3.1415926535897932384626433832795028841971693993751058209749445923078164062862
3.14159265358979323846264338327950288419716939937510582097494459230781640628620
3.141592653589793238462643383279502884197169399375105820974944592307816406286208
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Explanation
First make the range [0, n], shown for n = 5
i. >: 5
0 1 2 3 4 5
Multiply each by 8
(*&8) i. >: 5
0 8 16 24 32 40
Form the addition table between [1, 4, 5, 6]
and the products with 8
(1 4 5 6+/*&8) i. >: 5
1 9 17 25 33 41
4 12 20 28 36 44
5 13 21 29 37 45
6 14 22 30 38 46
Divide each row by [4, 2, -1, 1]
(4 2 _1 1%1 4 5 6+/*&8) i. >: 5
4 0.444444 0.235294 0.16 0.121212 0.097561
0.5 0.166667 0.1 0.0714286 0.0555556 0.0454545
_0.2 _0.0769231 _0.047619 _0.0344828 _0.027027 _0.0222222
0.166667 0.0714286 0.0454545 0.0333333 0.0263158 0.0217391
Then reduce the columns from bottom-to-top using subtraction
([:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 0.129426 0.0422205 0.0207553 0.0123137 0.00814508
Divide each 16k for k in [0, n] by each result
(16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 0.00808913 0.000164924 5.06722e_6 1.87893e_7 7.76775e_9
Find the cumulative sums
([:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 3.14142 3.14159 3.14159 3.14159 3.14159
Compute 10k for k in [0, n] and multiply it with each
(10&^(*)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3.13333 31.4142 314.159 3141.59 31415.9 314159
Then floor each of the products
(10&^(<.@*)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3 31 314 3141 31415 314159
Divide it by the same power of 10 to get the results
(10&^(<.@*%[)[:+/\16&^%~[:-/4 2 _1 1%1 4 5 6+/*&8) i. >: 5
3 3.1 3.14 3.141 3.1415 3.14159
5The only downside I see is that it will be difficult to verify exactly what method someone is using soley based on the output, which is generally a problem with
Calculate foo via x method
challenges. – James – 2016-10-27T17:11:58.200@DJMcMayhem Added an explanation of the code you post being required if it is not an obvious implementation, to ensure we can actually tell what they did. The algorithm is actually fairly straightforward though, so it shouldn't be too bad. – Magic Octopus Urn – 2016-10-27T17:14:34.913
@Emigna it should correctly calculate pi to
k
digits on each iteration using the algorithm provided. So the precision is important. – Magic Octopus Urn – 2016-10-27T17:27:41.2232
With respect to @DJMcMayhem's comment, see the advice to avoid non-observable program requirements.
– Peter Taylor – 2016-10-27T17:45:28.083@carusocomputing: What I meant was, is it okay to output more than n digits, where the digits after the nth may or may not be correct or if the result should be exactly n digits, but I removed the comment as it doesn't really matter. – Emigna – 2016-10-27T17:58:05.697
@Emigna the overall goal was to match the output provided using the algorithm specified for all
n
, probably would've been better to not use the rounding at all; as that would've made it more observable, but it's a bit late now. – Magic Octopus Urn – 2016-10-27T18:00:24.4402You must support n up to a maximum that your language allows. Allows how? Can I use recursion? Can I use lists if generators would be more memory-friendly? Can I use 2n digits and chop the last n off? – Dennis – 2016-10-27T18:58:23.233
1In the interest of clarity, I'd just remove the ordinals before that output that is actually required. – Dennis – 2016-10-27T20:08:28.637