40 lines
1.5 KiB
Java
40 lines
1.5 KiB
Java
package UE08_051124_Rekursion1;
|
|
|
|
public class UE08_Rekursion {
|
|
public static void main(String[] args) {
|
|
printFibonacciFolge(8);
|
|
System.out.println(zaehleSelbstlaute("Hallo du MENSCH"));
|
|
System.out.println(zahlHoch(2, 5));
|
|
System.out.println(zahlHoch(2, 10));
|
|
System.out.println(zahlHoch(1, 1));
|
|
System.out.println(zahlHoch(1, 10));
|
|
System.out.println(zahlHoch(5, 5));
|
|
System.out.println(zahlHoch(20, 3));
|
|
System.out.println(zahlHoch(10, 1));
|
|
System.out.println(zahlHoch(10, 10));
|
|
}
|
|
|
|
public static int fibonacci(int n) {
|
|
return n <= 2 ? n <= 0 ? 0 : 1 : fibonacci(n - 1) + fibonacci(n - 2);
|
|
}
|
|
|
|
public static void printFibonacciFolge(int anz) {
|
|
for (int i = 1; i <= anz; i++) System.out.print(fibonacci(i) + " ");
|
|
}
|
|
|
|
public static int zaehleSelbstlaute(String s) {
|
|
// if (s.isEmpty()) return 0;
|
|
// if ("aeiouAEIOU".indexOf(s.charAt(0)) == -1) return 0;
|
|
// return 1 + zaehleSelbstlaute(s.substring(1));
|
|
return s.isEmpty() ? 0 : (("aeiouAEIOU".indexOf(s.charAt(0)) == -1) ? 0 : 1) + zaehleSelbstlaute(s.substring(1));
|
|
}
|
|
|
|
public static long zahlHoch(int x, int n) {
|
|
// if (n <= 1) return x;
|
|
// long z = zahlHoch(x, n >> 1);
|
|
// if ((n & 1) != 0) return x * z * z;
|
|
// return z * z;
|
|
return n <= 1 ? x : ((n & 1) != 0 ? x * zahlHoch(x, n >> 1) * zahlHoch(x, n >> 1) : zahlHoch(x, n >> 1) * zahlHoch(x, n >> 1));
|
|
}
|
|
}
|