001 package de.hska.java.aufgaben.kontrollstrukturen;
002
003 /**
004 * Zum Berechnen einer Näehrungslösung der Kreiszahl
005 * Pi mit Hilfe des Wallisschen Produkts.
006 *
007 *
008 * <p>
009 * <a href="http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/kontrollanweisungen.html#wallissche_produkt">Zurück zum Aufgabentext</a>
010 * </p>
011 *
012 * @author Christian Pape
013 *
014 */
015 public class WallisscheProdukt {
016
017 /**
018 * Gibt eine Näherungslösung von Pi zurück. Dazu werden alle
019 * ersten n-Glieder des Wallisschen Produkts berechnet.
020 */
021 public static double piBerechnen(int n) {
022 double piHalbe = 1.0;
023 double j = 1.0;
024
025 // Pi / 2 = (2/1) * (2/3) * (4/3) * (4/5) * (6/5) * (6/7) * ..
026 for (double i = 1; i < n; i++, j++) {
027 if (i % 2 == 0) {
028 piHalbe *= (j / (j + 1));
029 } else {
030 piHalbe *= ((j + 1) / j);
031 }
032 }
033
034 return 2.0 * piHalbe;
035 }
036
037 public static void main(String argv[]) {
038 for (int i = 0; i < 1000; i++) {
039 System.out.println(WallisscheProdukt.piBerechnen(i));
040 }
041 System.out.println(Math.PI);
042 }
043 }