Almkvist-Giullera formula for pi: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(New post.) |
||
Line 1,518: | Line 1,518: | ||
pi to 70 decimals: 3.1415926535897932384626433832795028841971693993751058209749445923078164 |
pi to 70 decimals: 3.1415926535897932384626433832795028841971693993751058209749445923078164 |
||
</pre> |
|||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
import java.math.BigDecimal; |
|||
import java.math.BigInteger; |
|||
import java.math.MathContext; |
|||
import java.math.RoundingMode; |
|||
public final class AlmkvistGiulleraFormula { |
|||
public static void main(String[] aArgs) { |
|||
System.out.println("n Integer part"); |
|||
System.out.println("================================================"); |
|||
for ( int n = 0; n <= 9; n++ ) { |
|||
System.out.println(String.format("%d%47s", n, almkvistGiullera(n).toString())); |
|||
} |
|||
final MathContext mathContext = new MathContext(71, RoundingMode.HALF_EVEN); |
|||
final BigDecimal epsilon = BigDecimal.ONE.divide(BigDecimal.TEN.pow(70)); |
|||
BigDecimal previous = BigDecimal.ONE; |
|||
BigDecimal sum = BigDecimal.ZERO; |
|||
BigDecimal pi = BigDecimal.ZERO; |
|||
int n = 0; |
|||
while ( pi.subtract(previous).abs().compareTo(epsilon) >= 0 ) { |
|||
BigDecimal nextTerm = new BigDecimal(almkvistGiullera(n)).divide(BigDecimal.TEN.pow(6 * n + 3)); |
|||
sum = sum.add(nextTerm); |
|||
previous = pi; |
|||
n += 1; |
|||
pi = BigDecimal.ONE.divide(sum, mathContext).sqrt(mathContext); |
|||
} |
|||
System.out.println(System.lineSeparator() + "pi to 70 decimal places:"); |
|||
System.out.println(pi); |
|||
} |
|||
// The integer part of the n'th term of Almkvist-Giullera series. |
|||
private static BigInteger almkvistGiullera(int aN) { |
|||
BigInteger term1 = factorial(6 * aN).multiply(BigInteger.valueOf(32)); |
|||
BigInteger term2 = BigInteger.valueOf(532 * aN * aN + 126 * aN + 9); |
|||
BigInteger term3 = factorial(aN).pow(6).multiply(BigInteger.valueOf(3)); |
|||
return term1.multiply(term2).divide(term3); |
|||
} |
|||
private static BigInteger factorial(int aNumber) { |
|||
BigInteger result = BigInteger.ONE; |
|||
for ( int i = 2; i <= aNumber; i++ ) { |
|||
result = result.multiply(BigInteger.valueOf(i)); |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
n Integer part |
|||
================================================ |
|||
0 96 |
|||
1 5122560 |
|||
2 190722470400 |
|||
3 7574824857600000 |
|||
4 312546150372456000000 |
|||
5 13207874703225491420651520 |
|||
6 567273919793089083292259942400 |
|||
7 24650600248172987140112763715584000 |
|||
8 1080657854354639453670407474439566400000 |
|||
9 47701779391594966287470570490839978880000000 |
|||
pi to 70 decimal places: |
|||
3.1415926535897932384626433832795028841971693993751058209749445923078164 |
|||
</pre> |
</pre> |
||