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 Stream Filter with Lambda Expression
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Introduction to Spring Method Security
Spring MVC and the @ModelAttribute Annotation
Custom Cascading in Spring Data MongoDB
List Interface trong Java
Java Program to Implement VList
Guide to Java 8 groupingBy Collector
Java 8 – Powerful Comparison with Lambdas
Spring RestTemplate Request/Response Logging
Sử dụng CyclicBarrier trong Java
Java Program to Implement Network Flow Problem
Spring WebClient and OAuth2 Support
Understanding Memory Leaks in Java
Apache Commons Collections Bag
Cachable Static Assets with Spring MVC
Spring – Injecting Collections
Java Program to Implement WeakHashMap API
Java Program to Find a Good Feedback Vertex Set
Java Program to Implement Binary Heap
Apache Camel with Spring Boot
Guide to java.util.concurrent.Future
Hướng dẫn Java Design Pattern – Factory Method
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
An Intro to Spring Cloud Task
Truyền giá trị và tham chiếu trong java
So sánh ArrayList và Vector trong Java
Rate Limiting in Spring Cloud Netflix Zuul
Spring Boot - Introduction
Number Formatting in Java
Merging Streams in Java
Instance Profile Credentials using Spring Cloud