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;
}