Almkvist-Giullera formula for pi: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: reorganize) |
(→{{header|JavaScript}}: Add implementation) |
||
Line 643: | Line 643: | ||
Pi after 52 iterations: |
Pi after 52 iterations: |
||
3.1415926535897932384626433832795028841971693993751058209749445923078164</pre> |
3.1415926535897932384626433832795028841971693993751058209749445923078164</pre> |
||
=={{header|JavaScript}}== |
|||
{{trans|Common Lisp}} |
|||
{{works with|Node.js|13+}} |
|||
{{libheader|bigfloat-esnext}} |
|||
{{libheader|es-main}} to support use of module as main code |
|||
<lang javascript>import esMain from 'es-main'; |
|||
import { BigFloat, set_precision as SetPrecision } from 'bigfloat-esnext'; |
|||
const Iterations = 52; |
|||
export const demo = function() { |
|||
SetPrecision(-75); |
|||
console.log("N." + "Integral part of Nth term".padStart(45) + " ×10^ =Actual value of Nth term"); |
|||
for (let i=0; i<10; i++) { |
|||
let line = `${i}. `; |
|||
line += `${integral(i)} `.padStart(45); |
|||
line += `${tenExponent(i)} `.padStart(5); |
|||
line += nthTerm(i); |
|||
console.log(line); |
|||
} |
|||
let pi = approximatePi(Iterations); |
|||
SetPrecision(-70); |
|||
pi = pi.dividedBy(100000).times(100000); |
|||
console.log(`\nPi after ${Iterations} iterations: ${pi}`) |
|||
} |
|||
export const bigFactorial = n => n <= 1n ? 1n : n * bigFactorial(n-1n); |
|||
// the nth integer term |
|||
export const integral = function(i) { |
|||
let n = BigInt(i); |
|||
const polynomial = 532n * n * n + 126n * n + 9n; |
|||
const numerator = 32n * bigFactorial(6n * n) * polynomial; |
|||
const denominator = 3n * bigFactorial(n) ** 6n; |
|||
return numerator / denominator; |
|||
} |
|||
// the exponent for 10 in the nth term of the series |
|||
export const tenExponent = n => 3n - 6n * (BigInt(n) + 1n); |
|||
// the nth term of the series |
|||
export const nthTerm = n => |
|||
new BigFloat(integral(n)).dividedBy(new BigFloat(10n ** -tenExponent(n))) |
|||
// the sum of the first n terms |
|||
export const sumThrough = function(n) { |
|||
let sum = new BigFloat(0); |
|||
for (let i=0; i<=n; ++i) { |
|||
sum = sum.plus(nthTerm(i)); |
|||
} |
|||
return sum; |
|||
} |
|||
// the approximation to pi after n terms |
|||
export const approximatePi = n => |
|||
new BigFloat(1).dividedBy(sumThrough(n)).sqrt(); |
|||
if (esMain(import.meta)) |
|||
demo(); |
|||
</lang> |
|||
{{Out}} |
|||
<pre>N. Integral part of Nth term ×10^ =Actual value of Nth term |
|||
0. 96 -3 0.096 |
|||
1. 5122560 -9 0.00512256 |
|||
2. 190722470400 -15 0.0001907224704 |
|||
3. 7574824857600000 -21 0.0000075748248576 |
|||
4. 312546150372456000000 -27 0.000000312546150372456 |
|||
5. 13207874703225491420651520 -33 0.00000001320787470322549142065152 |
|||
6. 567273919793089083292259942400 -39 0.0000000005672739197930890832922599424 |
|||
7. 24650600248172987140112763715584000 -45 0.000000000024650600248172987140112763715584 |
|||
8. 1080657854354639453670407474439566400000 -51 0.0000000000010806578543546394536704074744395664 |
|||
9. 47701779391594966287470570490839978880000000 -57 0.00000000000004770177939159496628747057049083997888 |
|||
Pi after 52 iterations: 3.1415926535897932384626433832795028841971693993751058209749445923078164</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |