Based on a recursive implementation of the Chudnovsky algorithm, one of the fastest algorithms to estimate pi. For each iteration, roughly 14 digits are estimated (take a look here for further details).
def f(n):import decimal as d;d.getcontext().prec=n+10;q=lambda n,k=6,m=1,l=13591409,x=1,i=1:(d.Decimal(((k**3-16*k)*m//i**3)*(l+545140134))/(x*-262537412640768000)+q(n,k+12,(k**3-16*k)*m//i**3,l+545140134,x*-262537412640768000,i+1)if i<n else 13591409);return sum([ord(i)%2 and int(i)for i in str(426880*d.Decimal(10005).sqrt()/q(n//14+1))[:n+1]])
Ungolfed version
def f(n):
import decimal as d
d.getcontext().prec = n + 10
q=lambda n, k=6, m=1, l=13591409, x=1, i=1:\
(d.Decimal(((k ** 3 - 16 * k) * m // i**3) * (l + 545140134)) / (x * -262537412640768000) +
q(n, k+12, (k ** 3 - 16 * k) * m // i**3, l + 545140134, x * -262537412640768000, i+1)if i < n else 13591409)
return sum([ord(i) % 2 and int(i)for i in
str(426880 * d.Decimal(10005).sqrt() / q(n // 14+1))[:n + 1]])
Function q
computes the value of S
(see the reference), than it is used to divide the term 426880 * d.Decimal(10005).sqrt()
and get the value of the approximation of pi.
The precision is controlled by setting the value of d.getcontext().prec
.
A a little bit longer (372 bytes), more golfed but all-in-one version:
f=lambda n,k=6,m=1,l=13591409,x=1,i=0:not i and(exec('global d;import decimal as d;d.getcontext().prec=%d+10'%n)or sum([ord(i)%2 and int(i)for i in str(426880*d.Decimal(10005).sqrt()/f(n//14+1,k,m,l,x,1))[:n+1]]))or i<n and d.Decimal(((k**3-16*k)*m//i**3)*(l+545140134))/(x*-262537412640768000)+f(n,k+12,(k**3-16*k)*m//i**3,l+545140134,x*-262537412640768000,i+1)or 13591409
Thanks to O.O.Balance to push me to go deeper into this challenge: it was very fun and interesting.
Old answer, based on math.pi
(74 bytes)
import math;f=lambda n:sum([ord(i)%2and int(i)for i in"%.*G"%(n,math.pi)])
Try it online!
N.B.: math.pi
is "The mathematical constant π = 3.141592…, to available precision.", but the digits for the challenge are computed on the fly through the rounding and truncating operations. So, technically this is not a violation of the rules: "all the digits of pi must be calculated by your program at runtime."
Does the range include the first 3? Does the count start at that three (is it the first element or is it seperate from the count, i.e, the zeroth element)? – sagiksp – 2018-07-07T10:38:28.230
4@Mackan90096 Even though you've specified the output "Must be Calculated", this requirement is Non-observable, meaning it's difficult and case-by-case to determine if an answer is valid. It would be easier to define your challenge in such as way that hard-coding the answer isn't a good tactic, like having variable input. – ATaco – 2018-07-07T11:23:06.550
Edited to make more sense by making the range 1 to n, @ATaco – Mackan90096 – 2018-07-07T11:39:56.810
If n = 6, we must find the sum of the odd digits in {3, 1, 4, 1, 5, 9}, so 19? If this is what you mean, then in the case n=31,415 the sum equals 78,664, not 78,659 as you stated in the example. Please clarify. – sagiksp – 2018-07-07T12:14:46.897
2Also, I meant the three at the start, the first digit, not the first three digits like you wrote. – sagiksp – 2018-07-07T12:15:13.747
My mistake, I've gone ahead and fixed it, @sagiksp, and yes, including the first digit. – Mackan90096 – 2018-07-07T13:31:53.560
3All the digits of pi must be calculated by your program: does that mean no hardcoding of the first N digits of PI or no built-in at all? (Edit: apparently, this rule comes from the initial version of the challenge, where the number of digits was fixed to 31415. Now that the length is taken as input, it should probably be removed.) – Arnauld – 2018-07-07T14:08:12.030
How about
– Arnauld – 2018-07-07T14:39:57.910žs
in 05AB1E? Since the digits are technically hardcoded in the language source code, does that mean it's not allowed?11
"All the digits of pi must be calculated by your program" is a non-observable requirement, so I'd recommend removing it.
– Jonathan Allan – 2018-07-07T15:06:32.117The title is misleading. The sum of all digits of pi is infinity – Luis Mendo – 2018-07-08T16:44:53.090
The question suffers from unclear working. I think the author meant to imply no built-ins for
– None – 2018-07-09T03:02:47.240π
, i.e. you have to code the algorithm yourself. If this is the case, the question indicated as a duplicate is actually different, because built-ins are allowed in that question.As far as I know, the only way this challenge can be done is by computing all digits of π up to n'th digit. Also as far as I know, all the existing algorithms to compute π at n'th digit in decimal also compute π at all digits before position n. Therefore programs in the other challenge can be trivially ported into this one. I agree that it's a duplicate. – user202729 – 2018-07-09T03:25:10.160
This is the guideline to close as duplicate. In my opinion, computing π is much harder than filter out odd digits and take the sum, so (2) is true. (1) and (3) is obviously true. – user202729 – 2018-07-09T03:34:56.627