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 Implement Fermat Factorization Algorithm
Allow user:password in URL
Java Program to Implement Heap Sort Using Library Functions
Java Program to Implement Segment Tree
Java Program to Create a Random Graph Using Random Edge Generation
The HttpMediaTypeNotAcceptableException in Spring MVC
Spring Boot - Code Structure
Spring MVC Tutorial
Check if there is mail waiting
Java Program to Implement Solovay Strassen Primality Test Algorithm
A Comparison Between Spring and Spring Boot
A Quick Guide to Spring MVC Matrix Variables
A Guide to LinkedHashMap in Java
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Using Spring ResponseEntity to Manipulate the HTTP Response
Anonymous Classes in Java
Count Occurrences of a Char in a String
The Difference Between Collection.stream().forEach() and Collection.forEach()
How to Read a Large File Efficiently with Java
Call Methods at Runtime Using Java Reflection
A Guide To UDP In Java
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Pagination and Sorting using Spring Data JPA
Introduction to Spring Cloud Stream
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Giới thiệu Google Guice – Injection, Scope
How to Implement Caching using Adonis.js 5
Java Program to Find the Connected Components of an UnDirected Graph
Introduction to Eclipse Collections
Java Program to Find the Minimum value of Binary Search Tree