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:
Spring Data Java 8 Support
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Guide to the Java TransferQueue
Java Program to Compare Binary and Sequential Search
Java Program to Implement Ford–Fulkerson Algorithm
Spring Boot - Tomcat Port Number
Default Password Encoder in Spring Security 5
HTTP Authentification and CGI/Servlet
Java Program to Implement Insertion Sort
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
How to Manually Authenticate User with Spring Security
Java Program to Implement vector
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement Knapsack Algorithm
Exploring the Spring Boot TestRestTemplate
Spring Cloud – Securing Services
Send an email using the SMTP protocol
Guide to @JsonFormat in Jackson
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Read an Outlook MSG file
String Joiner trong Java 8
Entity To DTO Conversion for a Spring REST API
Integer Constant Pool trong Java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Implement Gauss Jordan Elimination
Java Program to Perform integer Partition for a Specific Case
Migrating from JUnit 4 to JUnit 5
Java – InputStream to Reader
Deploy a Spring Boot WAR into a Tomcat Server
Convert String to int or Integer in Java
Versioning a REST API