-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
public class CalculatePi { | ||
public static void main(String[] args) { | ||
int scale = 1000; | ||
BigDecimal pi = computePi(scale); | ||
System.out.println(pi); | ||
} | ||
|
||
private static BigDecimal computePi(int scale) { | ||
BigDecimal pi = BigDecimal.ZERO; | ||
BigDecimal term1 = BigDecimal.ONE; | ||
BigDecimal term2 = BigDecimal.ONE; | ||
BigDecimal term3 = BigDecimal.ONE; | ||
BigDecimal sign = BigDecimal.ONE; | ||
|
||
for (int i = 0; i < scale; i++) { | ||
BigDecimal divisor = new BigDecimal(2*i + 1); | ||
term1 = term1.multiply(new BigDecimal(i * 2)); | ||
term2 = term2.multiply(new BigDecimal(i * 2 + 1)); | ||
term3 = term3.multiply(new BigDecimal(i * 2 + 2)); | ||
BigDecimal term = term1.multiply(term2).divide(term3.multiply(divisor), scale, RoundingMode.HALF_UP); | ||
|
||
if (i % 2 == 0) { | ||
pi = pi.add(term.multiply(sign)); | ||
} else { | ||
pi = pi.subtract(term.multiply(sign)); | ||
} | ||
} | ||
|
||
return pi.multiply(new BigDecimal(2)); | ||
} | ||
} |