Apéry's constant: Difference between revisions

Content added Content deleted
(Added Algol 68)
Line 41: Line 41:
;* [[oeis:A002117|OEIS:A002117 - Decimal expansion of zeta(3)]]
;* [[oeis:A002117|OEIS:A002117 - Decimal expansion of zeta(3)]]



=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT and LONG LONG REAL which have programmer specified precision. The factorials can get quuite large so more than 101 digits are needed.
<syntaxhighlight lang="algol68">
BEGIN # find Apéry's constant: the sum of the positive cubes' reciprocals #
# (this is the value of the Riemann zeta function applied to 3) #

PR precision 1000 PR # set precision of LONG LONG REAL #

# returns a string representation of z, truncated to 100 decimals #
PROC truncate100 = ( LONG LONG REAL z )STRING:
BEGIN
STRING result = fixed( z, 0, 101 ); # format with 101 decimals #
result[ : UPB result - 1 ] # remove the final digit #
END # truncate100 # ;

# methind 1 - sum the reciprocols of the cubes - 1000 terms from 1 #
BEGIN
LONG LONG REAL zeta3 := 0;
FOR k TO 1000 DO
LONG LONG INT llk = LENG LENG k;
zeta3 +:= 1 / ( llk * llk * llk )
OD;
print( ( truncate100( zeta3 ), newline ) )
END;

# method 2 - Markov's alternative representaton, 158 terms from 1 #
# 5/2 * sum [ (-1)^(k-1)( k!^2 / (2k!)k^2 ) ] from 1 #
BEGIN
LONG LONG INT fk := 1, f2k := 1;
LONG LONG REAL zeta3 := 0;
FOR k TO 158 DO
LONG LONG INT llk = k;
LONG LONG INT ll2k = llk * 2;
fk *:= llk;
f2k *:= ( ll2k - 1 ) *:= ll2k;
LONG LONG REAL term = ( fk * fk ) / ( f2k * llk * llk * llk );
IF ODD k THEN
zeta3 +:= term
ELSE
zeta3 -:= term
FI
OD;
zeta3 *:= 5 / 2;
print( ( truncate100( zeta3 ), newline ) )
END;

# method 3 - Wedeniwski representation - 20 terms from 0 #
# 1/24 * sum [ (-1)^k (2k+1)!^3(2k)!^3(k!)^3 #
# * ( 126392k^5 + 412708k^4 + 531578k^3 #
# + 336367k^2 + 104000k + 12463 #
# ) / (3k+2)! (4k + 3)!^3 #
# ] from 0 #
BEGIN
[]INT w coefficients = ( 126392, 412708, 531578, 336367, 104000, 12463 );
LONG LONG INT fk := 1, f2k := 1, f3k := 1, f4k := 1;
# ensure the divisor is a LONG LONG INT so the LHS is calculated as a #
# LONG LONG REAL value and not a REAL which is then widened #
LONG LONG REAL zeta3 := w coefficients[ UPB w coefficients ]
/ LENG LENG ( 2 * 6 * 6 * 6 );
FOR k TO 19 DO
LONG LONG INT llk = k;
LONG LONG INT ll2k = llk + llk;
LONG LONG INT ll3k = ll2k + llk;
LONG LONG INT ll4k = ll3k + llk;
fk *:= llk;
f2k *:= ( ll2k - 1 ) * ll2k;
f3k *:= ( ll3k - 2 ) * ( ll3k - 1 ) * ll3k;
f4k *:= ( ll4k - 3 ) * ( ll4k - 2 ) * ( ll4k - 1 ) * ll4k;
LONG LONG INT f2k1 = f2k * ( ll2k + 1 );
LONG LONG INT f3k2 = f3k * ( ll3k + 1 ) * ( ll3k + 2 );
LONG LONG INT f4k3 = f4k * ( ll4k + 1 ) * ( ll4k + 2 ) * ( ll4k + 3 );
LONG LONG REAL term := 0;
FOR c TO UPB w coefficients DO
term *:= llk +:= w coefficients[ c ]
OD;
LONG LONG INT fp = f2k1 * f2k * fk;
term *:= fp * fp * fp /:= f3k2 * f4k3 * f4k3 * f4k3;
IF ODD k THEN
zeta3 -:= term
ELSE
zeta3 +:= term
FI
OD;
zeta3 /:= 24;
print( ( truncate100( zeta3 ), newline ) )
END

END
</syntaxhighlight>
{{out}}
<pre>
1.2020564036593442854830714115115999903483212709031775135036540966118572571921400836130084123260473111
1.2020569031595942853997381615114499907649862923404988817922715553418382057863130901864558736093352581
1.2020569031595942853997381615114499907649862923404988817922715553418382057863130901864558736093352581
</pre>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==