add UE09_Rekursion2

This commit is contained in:
AlexBa16
2024-11-21 08:53:31 +01:00
parent 617fe82664
commit 6cfc5bdac9
5 changed files with 102 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
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));
}
}