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 Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Simplify the DAO with Spring and Java Generics
Java Program to Implement Threaded Binary Tree
Bootstrapping Hibernate 5 with Spring
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Spring Cloud AWS – EC2
Java Program to Check whether Directed Graph is Connected using DFS
Java – Generate Random String
Java Program to Implement PriorityBlockingQueue API
Send email with SMTPS (eg. Google GMail)
Implementing a Binary Tree in Java
Giới thiệu Json Web Token (JWT)
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Getting Started with Forms in Spring MVC
Java 8 Stream findFirst() vs. findAny()
Java Program to Implement PriorityQueue API
Java Program to Perform Naive String Matching
4 tính chất của lập trình hướng đối tượng trong Java
XML-Based Injection in Spring
Jackson – Bidirectional Relationships
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Introduction to Thread Pools in Java
Java Program to Find the GCD and LCM of two Numbers
Hướng dẫn Java Design Pattern – Abstract Factory
Guide to the Java Queue Interface
String Processing with Apache Commons Lang 3
Using JWT with Spring Security OAuth (legacy stack)
Java Program to Solve the 0-1 Knapsack Problem
Guide to Apache Commons CircularFifoQueue
Java Program to Implement SynchronosQueue API
Lớp TreeMap trong Java