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:
Batch Processing with Spring Cloud Data Flow
Difference Between Wait and Sleep in Java
Guide to CountDownLatch in Java
Creating a Web Application with Spring 5
Spring Security Basic Authentication
New Features in Java 8
Spring Cloud Series – The Gateway Pattern
Apache Camel with Spring Boot
Spring Data Java 8 Support
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Implement PrinterStateReasons API
Setting the Java Version in Maven
Java Program to Decode a Message Encoded Using Playfair Cipher
Java Program to Implement the Monoalphabetic Cypher
Java Program to Implement Radix Sort
Converting Java Date to OffsetDateTime
Sending Emails with Java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to implement Circular Buffer
A Guide to the finalize Method in Java
Guide to the Fork/Join Framework in Java
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Implement Triply Linked List
The StackOverflowError in Java
Java Program to Create a Balanced Binary Tree of the Incoming Data
Hướng dẫn Java Design Pattern – DAO
Spring Boot - Admin Client
Notify User of Login From New Device or Location
Spring WebClient vs. RestTemplate
RegEx for matching Date Pattern in Java
A Guide to JPA with Spring
Java Program to Implement Ternary Heap