Java Program to Implement Cubic convergence 1/pi Algorithm

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:

Using a List of Values in a JdbcTemplate IN Clause
Show Hibernate/JPA SQL Statements from Spring Boot
Spring MVC and the @ModelAttribute Annotation
Documenting a Spring REST API Using OpenAPI 3.0
Overflow and Underflow in Java
Java Program to Perform Insertion in a BST
How to Kill a Java Thread
Introduction to Java Serialization
Apache Commons Collections SetUtils
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program for Topological Sorting in Graphs
Guide to java.util.Formatter
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
How to Find an Element in a List with Java
Java Program to Implement Segment Tree
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Setting Up Swagger 2 with a Spring REST API
ETL with Spring Cloud Data Flow
The Difference Between Collection.stream().forEach() and Collection.forEach()
Spring RestTemplate Request/Response Logging
Java Program to Implement Miller Rabin Primality Test Algorithm
Weak References in Java
Java Program to Implement Vector API
Serialization và Deserialization trong java
Hướng dẫn sử dụng Lớp FilePermission trong java
How to Break from Java Stream forEach
Java Program to Check Cycle in a Graph using Graph traversal
Practical Java Examples of the Big O Notation
Inject Parameters into JUnit Jupiter Unit Tests
Spring WebFlux Filters
Converting a Stack Trace to a String in Java
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java