The classic series ? = sum_0^\infty (-1^k)/ (2k+1)Can be written as: pi: 4*sum{ $[mod[x;2];-1;1]%1+2*x }orpi: 4 * sum {xexp[-1;x]%1+2*x}But it converges very slowly.q)pi each til 20000003.141592153589724q)pi each til 200000003.141592603589817In contrast we have Ramanujan’s fast result for pi (comments?)pi:{f:{prd 1f+til x}; 9801%sqrt[8]*sum{[x;f] f[4*x]*(1103+26390*x)%xexp[f;4]*xexp[396;4*x]}[;f]'[til x]}q)pi[1]3.141592730013306q)pi[2]3.141592653589794q)pi[3]3.141592653589793q)pi[4]3.141592653589793Maybe if we find a way in q (pique?) to print arbitrarily long stringsof the digits of pi…
2011/7/27 Rohit Tripathi <rohit.x.tripathi>:
> The classic series =F0 =3D sum_0^\infty (-1^k)/ (2k+1)
> Maybe if we find a way in q (pique?) to print arbitrarily long strings
> of the digits of pi…
i suspect we would need extended precision. see j:
http://www.jsoftware.com/jwiki/Essays/Extended%20Precision%20Functions
which would probably entail storing a list of digits and
defining + - * % and a lot of the other primitives as Xsum, Xdiff
Xprod and Xdiv for this type.
ta, jack.
>
> –
>
Submitted via Google Groups</rohit.x.tripathi>
On Jul 27, 5:20?am, Jack Andrews <effb…> wrote:> 2011/7/27 Rohit Tripathi <rohit.x.tripa…>:>> > The classic series ? = sum_0^\infty (-1^k)/ (2k+1)> > Maybe if we find a way in q (pique?) to print arbitrarily long strings> > of the digits of pi…>> i suspect we would need extended precision. ?see j:> ?http://www.jsoftware.com/jwiki/Essays/Extended%20Precision%20Functions>> which would probably entail storing a list of digits and> defining + - ?* % and a lot of the other primitives as Xsum, Xdiff> Xprod and Xdiv for this type.>> ta, jack.>I agree with you that q can benefit from extended precision to"display" large numbers,All I’m thinking of building a string with each iteration</rohit.x.tripa…></effb…>
Just realized that I never posted this
Of course for this specific problem a full algebra of bignums is not needed
Hey - there even exists a 52 byte machine code program which can compute this
direct translation of the algorithm at
http://www.codecodex.com/wiki/index.php?title=Digits_of_pi_calculation
yields something like this very unsightly scalar code
$cat pi.q
c:0 /carry
n:2800
S:10000 /SCALE
D:ceiling 2 xlog S
d:floor 10 xlog S
a:(n+1)#S div 5
i:n
while[i>0;
j:i;
s:0; /sum
while[j>0;
s:(s*j)+S*a j;
a[j]:s mod k:-1+2*j;
s:s div k;
j-:1];
1 d#string c+s div S;
c:s mod S;
i-:D];
-1"";
\
q$ q pi.q -q
31415926535897932384626433832795288241971693993751058209749445923078164062862089986280348253421170679821480865132823664670938446955958252317253594081284811174502841270219385211555596446229489549303819644288109756659334461284756482337867831652712019914956485669234634836104543266482133936072602491412737245870660663155881748815209209628292549179153643678925903611113305305488204665213841469519415116094330572736537595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566438608213949463952247371907021798694397027705392171762931767523846748184676694513520005681271452635608277857713427577896091736371787214684409012249534301465495853710579272796892589235420199561121290219686483443181598136297747713099605187072113499999983729780499510597317328160963185
just to show that we can ;)
Cheers,
Attila