This is a Java Program to Implement Cubic convergence 1/pi Algorithm. Cubic convergence is an algorithm used to calculate the value of 1/p.
Here is the source code of the Java Program to Implement Cubic convergence 1/pi Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to Implement Cubic convergence 1/pi Algorithm
**/
import java.util.Scanner;
/** Class CubicConvergencePi **/
public class CubicConvergencePi
{
/** compute 1/pi **/
public double getOneByPi(int k)
{
double ak = 1.0/3.0;
double sk = (Math.sqrt(3) - 1)/2;
double ak1, sk1, rk1;
for (int i = 0; i < k; i++)
{
rk1 = 3.0 / (1 + 2 * Math.pow((1 - sk * sk * sk), (1.0/3.0)));
sk1 = (rk1 - 1)/2.0;
ak1 = rk1 * rk1 * ak - Math.pow(3, i) * (rk1 * rk1 - 1);
ak = ak1;
sk = sk1;
}
return ak;
}
/** Main function **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Cubic Convergence 1/Pi Algorithm Test\n");
/** Make an object of CubicConvergence class **/
CubicConvergencePi cc = new CubicConvergencePi ();
System.out.println("Enter number of iterations");
int k = scan.nextInt();
System.out.println("\nValue of 1/pi : "+ cc.getOneByPi(k));
}
}
Output:
Cubic Convergence 1/Pi Algorithm Test Enter number of iterations 3 Value of 1/pi : 0.3183098861837896
Related posts:
Một số từ khóa trong Java
JUnit 5 for Kotlin Developers
Spring Boot - Bootstrapping
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Spring Boot - Sending Email
Java Program to Implement Adjacency Matrix
Adding Parameters to HttpClient Requests
Guide To CompletableFuture
Java – Combine Multiple Collections
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Expression Tree
Java Program to Solve a Matching Problem for a Given Specific Case
Get and Post Lists of Objects with RestTemplate
Handle EML file with JavaMail
The Difference Between map() and flatMap()
Convert String to int or Integer in Java
Thao tác với tập tin và thư mục trong Java
Java Program to Implement Patricia Trie
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Implement Weight Balanced Tree
Jackson Ignore Properties on Marshalling
Comparing Objects in Java
Marker Interface trong Java
Java Program to Implement Extended Euclid Algorithm
Remove HTML tags from a file to extract only the TEXT
Checking for Empty or Blank Strings in Java
Java Program to Implement Find all Back Edges in a Graph
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
An Intro to Spring Cloud Security
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Implement Kosaraju Algorithm