add UE09_Rekursion2

This commit is contained in:
AlexBa16
2024-11-29 13:57:35 +01:00
parent 830acf091c
commit 873c0e4537
2 changed files with 15 additions and 11 deletions

View File

@@ -3,7 +3,6 @@ package UE09_191124_Rekursion2;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class UE09_Rekursion2 {
@@ -14,7 +13,10 @@ public class UE09_Rekursion2 {
// System.out.println("\n" + maxTotalRekursion(file1, 0, 0));
// System.out.println("\n" + maxTotalRekursion(file2, 0, 0));
// System.out.println("\n" + maxTotalRekursion(file3, 0, 0)); //Braucht ewig lange, da File sehr groß
System.out.println(collatz());
// System.out.println(longestCollatzSequence(999999));
// System.out.println(longestCollatzSequence(13));
// System.out.println(collatzSequenceLength(13));
System.out.println(coinSums(2));
}
public static int[][] readFile(String file) {
@@ -36,10 +38,10 @@ public class UE09_Rekursion2 {
return (x >= input.length - 1) ? input[x][y] : Long.max(maxTotalRekursion(input, x + 1, y), maxTotalRekursion(input, x + 1, y + 1)) + input[x][y];
}
public static long collatz() {
public static long longestCollatzSequence(int limit) {
long startingNumber = 0, maxLength = 0;
for (int i = 1; i < 1000000; i++) {
long length = longestCollatzSequence(new ArrayList<>(), i).size();
for (int i = 1; i <= limit; i++) {
long length = collatzSequenceLength(i);
if (length > maxLength) {
maxLength = length;
startingNumber = i;
@@ -48,10 +50,12 @@ public class UE09_Rekursion2 {
return startingNumber;
}
public static List<Long> longestCollatzSequence(List<Long> l, long n) {
if (n == 1) return l;
l.add(n);
n = ((n & 1) == 0) ? (n / 2) : (3 * n + 1);
return longestCollatzSequence(l, n);
public static long collatzSequenceLength(long n) {
return n == 1 ? 1 : collatzSequenceLength((n & 1) == 0 ? n / 2 : 3 * n + 1) + 1;
}
public static long coinSums(int coin) {
return 0;
}
}