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 Depth-limited Search
Autoboxing và Unboxing trong Java
The XOR Operator in Java
Guide to System.gc()
Checked and Unchecked Exceptions in Java
How To Serialize and Deserialize Enums with Jackson
Java Program to Implement Attribute API
Java Program to Perform Arithmetic Operations on Numbers of Size
Hướng dẫn sử dụng Java Reflection
Converting Between a List and a Set in Java
Static Content in Spring WebFlux
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Best-First Search
Logging in Spring Boot
How to use the Spring FactoryBean?
Java Program to Implement Hash Tree
An Introduction to ThreadLocal in Java
A Guide to Queries in Spring Data MongoDB
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Handle EML file with JavaMail
@Lookup Annotation in Spring
Apache Commons Collections Bag
A Guide to Spring Cloud Netflix – Hystrix
Dockerizing a Spring Boot Application
Serialization và Deserialization trong java
How to Manually Authenticate User with Spring Security
Guide to Apache Commons CircularFifoQueue
Jackson Ignore Properties on Marshalling
Java Program to Check Whether Graph is DAG
Spring Data MongoDB Transactions
Java Program to Implement Sorted Singly Linked List
Abstract class và Interface trong Java