Java Program to Implement Cubic Convergence 1/pi Algorithm

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.

  1. /**
  2.  ** Java Program to Implement Cubic convergence 1/pi Algorithm
  3.  **/
  4. import java.util.Scanner;
  5.  
  6. /** Class CubicConvergencePi **/
  7. public class CubicConvergencePi
  8. {
  9.     /** compute 1/pi **/
  10.     public double getOneByPi(int k)
  11.     {
  12.         double ak = 1.0/3.0;
  13.         double sk = (Math.sqrt(3) - 1)/2;
  14.         double ak1, sk1, rk1;
  15.         for (int i = 0; i < k; i++)
  16.         {
  17.             rk1 = 3.0 / (1 + 2 * Math.pow((1 - sk * sk * sk), (1.0/3.0)));
  18.             sk1 = (rk1 - 1)/2.0;
  19.             ak1 = rk1 * rk1 * ak - Math.pow(3, i) * (rk1 * rk1 - 1);
  20.             ak = ak1;
  21.             sk = sk1;
  22.         }
  23.         return ak;        
  24.     }
  25.     /** Main function **/
  26.     public static void main (String[] args) 
  27.     {
  28.         Scanner scan = new Scanner(System.in);
  29.         System.out.println("Cubic Convergence 1/Pi Algorithm Test\n");
  30.         /** Make an object of CubicConvergence class **/
  31.         CubicConvergencePi  cc = new CubicConvergencePi ();
  32.  
  33.         System.out.println("Enter number of iterations");
  34.         int k = scan.nextInt();
  35.  
  36.         System.out.println("\nValue of 1/pi : "+ cc.getOneByPi(k));
  37.     }
  38. }

Cubic Convergence 1/Pi Algorithm Test
 
Enter number of iterations
3
 
Value of 1/pi : 0.3183098861837896

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.