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:
Spring Security Custom AuthenticationFailureHandler
Custom Thread Pools In Java 8 Parallel Streams
Hướng dẫn Java Design Pattern – State
Java Program to Implement Expression Tree
Java Program to Check if it is a Sparse Matrix
Java Program to Find Number of Articulation points in a Graph
Class Loaders in Java
Java Program to Implement Segment Tree
Java Program to Implement Interval Tree
Receive email using POP3
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Generate Spring Boot REST Client with Swagger
Debug a JavaMail Program
Java Program to Implement Queue
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Logout in an OAuth Secured Application
Java Program to Create a Random Linear Extension for a DAG
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Guide to ThreadLocalRandom in Java
Java Program to Implement Nth Root Algorithm
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Program to Implement Euler Circuit Problem
Retrieve User Information in Spring Security
Hướng dẫn Java Design Pattern – Builder
XML Serialization and Deserialization with Jackson
Java Program to Implement Brent Cycle Algorithm
Spring WebClient Requests with Parameters
Introduction to Java Serialization
How to Remove the Last Character of a String?
Một số nguyên tắc, định luật trong lập trình
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Check Cycle in a Graph using Topological Sort