Java Program to Implement Borwein Algorithm

«
»
This is a Java Program to Implement Borwein Algorithm. Borwein’s algorithm is an algorithm devised by Jonathan and Peter Borwein to calculate the value of 1/π.

Here is the source code of the Java Program to Implement Borwein 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 Borwein Algorithm
  3.  **/
  4. import java.util.Scanner;
  5.  
  6. /** Class Borwein **/
  7. public class Borwein
  8. {
  9.     /** compute 1/pi **/
  10.     public double getOneByPi(int k)
  11.     {
  12.         double ak = 6.0 - 4 * Math.sqrt(2);
  13.         double yk = Math.sqrt(2) - 1.0;
  14.  
  15.         double ak1 ;
  16.         double yk1 ;
  17.         for (int i = 0; i < k; i++)
  18.         {
  19.             yk1 = (1 - Math.pow((1 - yk * yk * yk * yk),(0.25)))/(1 + Math.pow((1 - yk * yk * yk * yk),(0.25)));
  20.             ak1 = ak * Math.pow((1 + yk1), 4) - Math.pow(2, 2 * i + 3) * yk1 * (1 + yk1 + yk1 * yk1);
  21.             yk = yk1;
  22.             ak = ak1;
  23.         }
  24.         return ak;
  25.     }
  26.     /** Main function **/
  27.     public static void main (String[] args) 
  28.     {
  29.         Scanner scan = new Scanner(System.in);
  30.         System.out.println("Borwein 1/Pi Algorithm Test\n");
  31.         /** Make an object of Borwein class **/
  32.         Borwein b = new Borwein();
  33.  
  34.         System.out.println("Enter number of iterations ");
  35.         int k = scan.nextInt();
  36.  
  37.         System.out.println("\nValue of 1/pi : "+ b.getOneByPi(k));
  38.     }
  39. }

advertisement
Borwein 1/Pi Algorithm Test
 
Enter number of iterations
5
 
Value of 1/pi : 0.31830988618379075

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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 & technical discussions at Telegram SanfoundryClasses.