Two methods of computing Pi Pi by Java implementation of the solution of Pi

Source: Internet
Author: User
method One: Cutting round method The calculation formula is:

Π≈3*2^n*y_n

wherein, n represents the number of cutting circles, and the y_n represents the edges of the embedded positive 6*n in the circle.

Package calculates the approximate value of π;
Import Java.util.Scanner;
public class Example {public
static void Main (string[] args) {
	Scanner scan=new Scanner (system.in);
	SYSTEM.OUT.PRINTLN ("Please input the number of cutting circle:");
	int N=scan.nextint ();
	Cut (n);
	
}
static void cut (int n) {
	double y=1.0;
	for (int i=0;i<=n;i++) {
		Doubleπ=3*math.pow (2, i) *y;
		System.out.println ("The first" +i+ "cut, is positive" + (6+6*i) + "edge shape, pi π≈" +π);
		Y=math.sqrt (2-math.sqrt (4-y*y));}}



method Two: Infinite series method

the series formula for Pi Pi is:


Package calculates the approximate value of π;
Import Java.util.Scanner;
public class Example1 {public
	static void Main (string[] args) {
          Scanner scan=new Scanner (system.in);
          SYSTEM.OUT.PRINTLN ("Please input accuracy:");
         Double z=scan.nextdouble ();
         System.out.println ("In the case of a precision of +z+", π is approximately equal to: \ n "+jishupi (z));
		
	}
	Static double Jishupi (double z) {
		double sum=2;
		int n=1;
		int m=3;
		Double t=2;
		while (t>z) {
			t=t*n/m;
			sum=sum+t;
			n++;
			m+=2;
			
			
		
		}
		return sum;
	}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.