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:
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java InputStream to String
Java Program to Implement the Program Used in grep/egrep/fgrep
Query Entities by Dates and Times with Spring Data JPA
Multi Dimensional ArrayList in Java
Java Program to Perform Partial Key Search in a K-D Tree
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Ways to Iterate Over a List in Java
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Implement Gauss Jordan Elimination
An Intro to Spring Cloud Task
Abstract class và Interface trong Java
Registration with Spring Security – Password Encoding
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Configuring a DataSource Programmatically in Spring Boot
A Guide to Concurrent Queues in Java
Spring Webflux and CORS
Jackson – Marshall String to JsonNode
Summing Numbers with Java Streams
Spring Boot - Creating Docker Image
Life Cycle of a Thread in Java
Java 8 Streams peek() API
Thao tác với tập tin và thư mục trong Java
Java Program to Solve any Linear Equations
Java Program to Implement Rope
Introduction to Spliterator in Java
Guide to CountDownLatch in Java
Java – Write to File
Exception Handling in Java
Spring AMQP in Reactive Applications
Java Program to Implement Max Heap
Returning Image/Media Data with Spring MVC