/** GeomReihe * calculates geomReihe elements */ public class GeomReihe { public static double geomReiheIterativ(double first, double q, int n) { double ergebnis = first; for (int i = 1; i < n; i++) { ergebnis *= q; } return ergebnis; } public static double geomReiheRekursiv(double first, double q, int n) { if (n == 1) return first; return q * geomReiheRekursiv(first, q, n - 1); } public static void main(String[] args) { double[] testQs = { 0.1, 0.9, 1.0, 1.5}; double first = 2.0; final int numberOfNs = 10; for (int j = 0; j < testQs.length; j++) { System.out.printf(">>>>>> neues q=%g\n",testQs[j]); for (int n = 1; n <= numberOfNs; n++) { System.out.printf("Iterativ: n=%d first=%g q=%g ergebnis=%g\n", n, first, testQs[j], geomReiheIterativ(first, testQs[j], n)); System.out.printf("Rekursiv: n=%d first=%g q=%g ergebnis=%g\n", n, first, testQs[j], geomReiheRekursiv(first, testQs[j], n)); } } } // EO main }