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:
Java Program to Implement Radix Sort
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement WeakHashMap API
Java Program to Implement Hash Tables with Quadratic Probing
Introduction to Spring Security Expressions
Compare Two JSON Objects with Jackson
Enum trong java
Java Program to Implement HashTable API
Inject Parameters into JUnit Jupiter Unit Tests
Spring MVC Content Negotiation
Using Optional with Jackson
How to Replace Many if Statements in Java
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java toString() Method
Java Program to Check the Connectivity of Graph Using DFS
Spring Security 5 – OAuth2 Login
Spring Boot - Building RESTful Web Services
Java Program to Implement Interpolation Search Algorithm
Spring Cloud AWS – S3
HttpClient Basic Authentication
Spring Security OAuth Login with WebFlux
A Guide to TreeMap in Java
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Guava Collections Cookbook
XML Serialization and Deserialization with Jackson
Java 8 StringJoiner
Introduction to Java 8 Streams
A Guide to LinkedHashMap in Java
Converting a List to String in Java
How to Get a Name of a Method Being Executed?
Spring Security and OpenID Connect
Spring Boot With H2 Database