Chudnovsky algorithm
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
1 post
• Page 1 of 1
Chudnovsky algorithm
The Chudnovsky algorithm is a fast method for computing the digits of pi. Each term of the series produces an additional 14 decimal digits.
To facilitate its implementation in APL, we rewrite the formula so that the terms are rational.
For example:
pica uses operator Q and function qi of the Rational Arithmetic facility. The code is available from here.
Substantially the same text previously appeared as the J wiki essay Chudnovsky Algorithm, 2011-05-08.
To facilitate its implementation in APL, we rewrite the formula so that the terms are rational.
⍝
pica←{
k ← qi ⍳⍵
top ← (!Q k×qi 6) ×Q (qi 13591409) +Q k ×Q qi 545140134
bot ← (!Q k×qi 3) ×Q ((!Q k)*Q qi 3) ×Q (qi 640320) *Q k ×Q qi 3
rt ← {(qi 2) ÷Q⍨ ⍵ +Q (qi 640320) ÷Q ⍵}⍣(⌈2⍟1+⍵) qi 640320*0.5
⍝ sqrt 640320 to 14×⍵ digits
÷Q ((qi 12) ÷Q (qi 640320) ×Q rt) ×Q -Q/ top ÷Q bot
}
For example:
100 ⍕Q pica 8
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170680
⍝ OEIS A000796
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982
pica uses operator Q and function qi of the Rational Arithmetic facility. The code is available from here.
Substantially the same text previously appeared as the J wiki essay Chudnovsky Algorithm, 2011-05-08.
- Roger|Dyalog
- Posts: 238
- Joined: Thu Jul 28, 2011 10:53 am
1 post
• Page 1 of 1
Who is online
Users browsing this forum: Bing [Bot] and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group