Java : Calculate Value of PI using Math Formulas-Part-1
Hi All,
First of all, Let me wish you a Very Happy New year 2023 . May you reach all your personal and professional goals.
Today , We will Calculate Value of PI using different methods in Java.
π (pronounced “pi”) is a mathematical constant that represents the ratio of the circumference of a circle to its diameter. It is a fundamental constant in mathematics and has many important applications in geometry, trigonometry, and calculus.
The value of π is approximately equal to 3.14159.
However , it is an irrational number, which means that it cannot be expressed exactly as a fraction. As a result, the value of π is usually approximated to a certain number of decimal places.
The value of π has been known and studied for thousands of years, and many different methods have been developed to calculate it with high precision. Some famous examples of formulas for calculating the value of π include the Leibniz formula, the Monte Carlo method, the Bailey–Borwein–Plouffe formula, and the Srinivasa Ramanujan formula,Newotn Formula etc.
In Modern Mathematics, π is represented by the symbol “π” and is often used in formulas to represent the value of the ratio of the circumference of a circle to its diameter. For example, the formula for the circumference of a circle is given by:
C = 2 * π * r
where C
is the circumference, π
is the value of π, and r
is the radius of the circle.
There are multiple ways to calculate value of PI in Mathematics .
In this post , We will Calculate Value of PI using different methods in java. This will be a 2 part post . In the first part , we will calculate PI using Normal method , Leibniz Formula, Monte Carlo method and Bailey–Borwein–Plouffe (BBP) formula in Java.
To calculate the value of π (pi) to a large number of decimal places in Java, you can use the BigDecimal
class and the MathContext
class from the java.math
package.
Here is an example of how you can calculate the value of π up to 100 decimal places:
package com.grp.connect.math.pi;
import java.math.BigDecimal;
import java.math.MathContext;
public class PICalculator1 {
public static void main(String[] args) {
// Set the precision to 100 decimal places
MathContext mc = new MathContext(100);
// Use the value of PI from the Math class as a starting point
BigDecimal pi = new BigDecimal(Math.PI, mc);
// Print the value of PI up to 100 decimal places
System.out.println(pi);
}
}
This will print the value of π to 100 decimal places, which is approximately:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Note that the value of π calculated using this method is only an approximation, as the true value of π is an irrational number that cannot be represented exactly as a decimal.
You can also use other methods to calculate the value of π to a large number of decimal places, such as the Leibniz formula or the Monte Carlo method.
Calculate the value of π using the Leibniz formula
To calculate the value of π using the Leibniz formula in Java, you can use a loop to iterate over the terms of the formula and use the BigDecimal
class from the java.math
package to store the intermediate and final results with a high degree of precision.
The Leibniz formula for π is an infinite series that can be expressed as:
π = 4/1–4/3 + 4/5–4/7 + 4/9–4/11 + …
Each term in the series is calculated by dividing 4 by an odd integer and adding or subtracting the result based on the parity of the term (odd or even). The sum of the terms of the series approaches the value of π as the number of terms increases.
Here is an example of how you can use the Leibniz formula to calculate the value of π to 100 decimal places in Java:
package com.grp.connect.math.pi;
import java.math.BigDecimal;
import java.math.MathContext;
public class PICalculator2 {
public static void main(String[] args) {
// Set the precision to 100 decimal places
MathContext mc = new MathContext(100);
// Initialize the sum to 0
BigDecimal sum = new BigDecimal(0, mc);
// Set the number of terms to calculate
int n = 10000;
// Loop over the terms of the series
for (int i = 1; i <= n; i++) {
// Calculate the current term of the series
BigDecimal term = new BigDecimal(4, mc).divide(new BigDecimal(2 * i - 1, mc), mc);
// Add or subtract the term based on the parity
if (i % 2 == 0) {
sum = sum.subtract(term, mc);
} else {
sum = sum.add(term, mc);
}
}
// Print the value of PI to 100 decimal places
System.out.println(sum);
}
}
This will print the value of π to 100 decimal places, which is an approximation of the true value of π. You can increase the number of terms (n
) to improve the accuracy of the approximation.
Note that the Leibniz formula is just one of many methods for calculating the value of π to a large number of decimal places. Other methods include the Monte Carlo method and the Bailey–Borwein–Plouffe formula.
To calculate the value of π using the Monte Carlo method
To calculate the value of π to a large number of decimal places using the Monte Carlo method in Java, you can use the Random
class from the java.util
package to generate random points within a unit square and count the number of points that fall within a quarter-circle inscribed within the square. The ratio of the number of points inside the quarter-circle to the total number of points is an approximation of π/4. By multiplying this ratio by 4, you can obtain an approximation of π.
Here is an example of how you can use the Monte Carlo method to calculate the value of π to 100 decimal places in Java:
package com.grp.connect.math.pi;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Random;
public class PICalculator3 {
public static void main(String[] args) {
// Set the precision to 100 decimal places
MathContext mc = new MathContext(100);
// Set the number of points to generate
int n = 1000000;
// Create a random number generator
Random rng = new Random();
// Initialize the counters for points inside and outside the quarter-circle
int inside = 0;
int outside = 0;
// Generate the points and count them
for (int i = 0; i < n; i++) {
// Generate a random point within the unit square
double x = rng.nextDouble();
double y = rng.nextDouble();
// Check if the point is inside the quarter-circle
if (x * x + y * y < 1) {
inside++;
} else {
outside++;
}
}
// Calculate the ratio of points inside the quarter-circle to the total number of points
BigDecimal ratio = new BigDecimal(inside, mc).divide(new BigDecimal(n, mc), mc);
// Multiply the ratio by 4 to obtain an approximation of PI
BigDecimal pi = ratio.multiply(new BigDecimal(4, mc), mc);
// Print the value of PI to 100 decimal places
System.out.println(pi);
}
}
This will print the value of π up to 100 decimal places, which is an approximation of the true value of π. You can increase the number of points (n) to improve the accuracy of the approximation.
Note that the Monte Carlo method is just one of many methods for calculating the value of π to a large number of decimal places. Other methods include the Leibniz formula and the Bailey–Borwein–Plouffe formula.
Calculate the value of π with Bailey–Borwein–Plouffe formula
To calculate the value of π to a large number of decimal places using the Bailey–Borwein–Plouffe (BBP) formula in Java, you can use the BigDecimal
class from the java.math
package to store the intermediate and final results with a high degree of precision.
The BBP formula for π is an infinite series that can be expressed as:
π = ∑[4/(8k + 1) — 2/(8k + 4) — 1/(8k + 5) — 1/(8k + 6)]
where k is a positive integer and the sum is taken over all values of k. Each term in the series is calculated by dividing a series of constants by a power of 16 and adding or subtracting the result. The sum of the terms of the series approaches the value of π as the number of terms increases.
Here is an example of how you can use the BBP formula to calculate the value of π to 100 decimal places in Java:
package com.grp.connect.math.pi;
import java.math.BigDecimal;
import java.math.MathContext;
public class PICalculator4 {
public static void main(String[] args) {
// Set the precision to 100 decimal places
MathContext mc = new MathContext(100);
// Initialize the sum to 0
BigDecimal sum = new BigDecimal(0, mc);
// Set the number of terms to calculate
int n = 100;
// Loop over the terms of the series
for (int k = 0; k < n; k++) {
// Calculate the current term of the series
BigDecimal term1 = new BigDecimal(4, mc).divide(new BigDecimal(8 * k + 1, mc), mc);
BigDecimal term2 = new BigDecimal(2, mc).divide(new BigDecimal(8 * k + 4, mc), mc);
BigDecimal term3 = new BigDecimal(1, mc).divide(new BigDecimal(8 * k + 5, mc), mc);
BigDecimal term4 = new BigDecimal(1, mc).divide(new BigDecimal(8 * k + 6, mc), mc);
BigDecimal term = term1.subtract(term2, mc).subtract(term3, mc).subtract(term4, mc);
// Add the term to the sum
sum = sum.add(term, mc);
}
// Print the value of PI to 100 decimal places
System.out.println(sum);
}
}
This Program will print the value of π to 100 decimal places, which is an approximation of the true value of π. You can increase the number of terms (n
) to improve the accuracy of the approximation.
Note that the BBP formula is just one of many methods for calculating the value of π to a large number of decimal places. Other methods include the the Srinivasa Ramanujan formula , the Newton Formula , Using Matrix Multiplication, Using (Fast Fourier Transform (FFT) etc.
We hope you liked this post of knowing PI with slightly more closely with java . These are just a few ways in which the PI can be calculated . PI is a fascinating and mysterious concept that continues to captivate the imagination of scientists and non-scientists alike.
Our Next Post will be focusing on calculating the value of π Other methods like the the Srinivasa Ramanujan formula , the Newton Formula , Using Matrix Multiplication, Using (Fast Fourier Transform (FFT) etc.
Happy Learning … Happy Coding …..
Other Interesting Articles:
Java : Understanding The Golden Ration Phi
AWS Learning : Journey towards Limitless Opportunities in Cloud .
No-cost ways to learn AWS Cloud over the holidays
Understanding 𝗖𝗢𝗥𝗦-𝗖𝗿𝗼𝘀𝘀-𝗢𝗿𝗶𝗴𝗶𝗻 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲 𝗦𝗵𝗮𝗿𝗶𝗻𝗴