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:
Lớp lồng nhau trong java (Java inner class)
Control Structures in Java
REST Web service: Basic Authentication trong Jersey 2.x
Intersection of Two Lists in Java
Send email with authentication
Tiêu chuẩn coding trong Java (Coding Standards)
Java String to InputStream
Map to String Conversion in Java
Java Program to Implement Stack using Linked List
Immutable Map Implementations in Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Giới thiệu Json Web Token (JWT)
Server-Sent Events in Spring
Spring Boot - Introduction
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Generic Constructors in Java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement VList
Default Password Encoder in Spring Security 5
Iterable to Stream in Java
Spring Boot - File Handling
Java Program to Perform Deletion in a BST
Spring 5 and Servlet 4 – The PushBuilder
Apache Tiles Integration with Spring MVC
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java Program to subtract two large numbers using Linked Lists
Database Migrations with Flyway
@Lookup Annotation in Spring
Jackson JSON Views
Disable Spring Data Auto Configuration
Java Program to Implement Self Balancing Binary Search Tree