x' = sqrt(2-sqrt(4 - x²)) |
Ecken Winkel 1/2 Kantenlänge=sin(Winkel) 4 45 0,707106781 8 22,5 0,382683432 16 11,25 0,195090322 32 5,625 0,09801714 64 2,8125 0,049067674 128 1,40625 0,024541229 256 0,703125 0,012271538 |
r Pi 1 1.0 2 2.25 4 2.8125 8 3.015625 16 3.09765625 32 3.1298828125 64 3.136962890625 128 3.13897705078125 256 3.1411285400390625 512 3.141284942626953 |
U = 2 * Pi * r ; r=1 ; Pi = U/2 Näherungsverfahren für den Umfang U: Wir starten mit einem Quadrat mit der Kantenlänge x und wir verdoppeln die Anzahl der Ecken. Nun gilt es dei Kantenlänge x' zu bestimmen: x = sqrt(2) a = sqrt(1-x²/4) b = 1 - a x' = sqrt(b² + x²/4) x' = sqrt(1- 2a+a² + x²/4) x' = sqrt(1- 2*sqrt(1-x²/4) + 1 -x²/4 + x²/4) x' = sqrt(2- sqrt(4-x²)) Und für weiter Kantenanzahl gilt: 4 Ecken: Pi ~ 2 * x 8 Ecken: Pi ~ 4 * x' 16 Ecken: Pi ~ 8 * x'' ... |
hier gilt: x/2 / x' = x' / c c = 2 * x'* x' / x Und hier gilt: 4 Ecken: Pi ~ 2 * c 8 Ecken: Pi ~ 4 * c' 16 Ecken: Pi ~ 8 * c'' ... |
import static java.lang.Math.pow; import static java.lang.Math.sqrt; import java.math.BigInteger; /** * * @author Rolf */ public class Pi { //-------------------------------------------------------------------------- static String niceNumber(int x) { String NS = " " + x; int l = NS.length(); return NS.substring(l - 12); } //-------------------------------------------------------------------------- static String nice(double x) { String NS = x+" "; return NS.substring(0, 16); } //-------------------------------------------------------------------------- static double doit(double x) { // return sqrt(2.0 - sqrt(4.0 - x * x)); // improved formula with less errors summary return x/sqrt(2.0 + sqrt(4.0 - x * x)); } //-------------------------------------------------------------------------- public static void out(BigInteger X) { String s = X.toString(); int n = s.length(); for (int i = 0; i < n; i += 80) { int max = i + 81; if (max > n) { max = n; } System.out.println(s.substring(i, max)); } } //-------------------------------------------------------------------------- public static void main(String[] args) { System.out.println("start to calc PI"); double pi, x,xx, PiOut; x = sqrt(2.0); System.out.println(" # corners \tinner approx. PI\t\tquality\t\touter appox. Pi"); for (int i = 2; i < 24; i++) { xx = doit(x); PiOut= pow(2,i)*2.0*xx*xx/x; x=xx; pi = pow(2, i) * xx; System.out.println(niceNumber((int) pow(2, i + 1)) + "\t" + nice(pi) + "\t\t" + (int) Math.abs(Math.log10(Math.abs(Math.PI - pi))) + "\t\t" +nice(PiOut) ); } System.out.println("\nreal Math.pi\t" + nice(Math.PI)+"\n\n"); System.out.println("\n\nNilakantha series. "); System.out.println("calculat according to PI = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14) ..."); /* Nilakantha series. This is another infinite series to calculate pi that is fairly easy to understand. While somewhat more complicated, it converges on pi much quicker than the Leibniz formula. π = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14) ... */ BigInteger PI = new BigInteger("0"); BigInteger b4 = new BigInteger("400000000000000000"); BigInteger d2 = new BigInteger("0"); BigInteger d3 = new BigInteger("0"); BigInteger d4 = new BigInteger("2"); int maxIter =5000; //X=X.add(b4).add(b4).add(b4); // + 4/(2*4*4); for (int i = 1; i < maxIter; i++) { d2 = d4; d3 = d2.add(new BigInteger("1")); d4 = d3.add(new BigInteger("1")); PI = PI.add(b4.divide(d2.multiply(d3).multiply(d4))); d2 = d4; d3 = d2.add(new BigInteger("1")); d4 = d3.add(new BigInteger("1")); PI = PI.subtract(b4.divide(d2.multiply(d3).multiply(d4))); } System.out.println("maxIter = "+(2*maxIter)+" steps later:"); System.out.print("\t\t3.");out(PI); System.out.println("according to wikipedia..."); System.out.println("\t\t3,141592653589793238462643...."); } } |