Created
May 23, 2020 11:51
-
-
Save timble-one/2b33ed0c3a386604a98ab3672275edc9 to your computer and use it in GitHub Desktop.
Gollatz Timo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Collatz36 { | |
| public static void main(String[] args) { | |
| int groupIndex = 0; | |
| int smallestGroupNumber = 0; | |
| int biggestGroupNumber = 0; | |
| for (int n = 101; n < 1000000; n++) { | |
| int stepIndex = 0; | |
| int step = n; | |
| while (stepIndex < 37 && step != 1) { | |
| step = collatz(step); | |
| stepIndex++; | |
| } | |
| if (stepIndex == 36) { | |
| groupIndex++; | |
| if (smallestGroupNumber == 0) { | |
| smallestGroupNumber = n; | |
| } | |
| if (n > biggestGroupNumber) { | |
| biggestGroupNumber = n; | |
| } | |
| } | |
| } | |
| System.out.printf("%s numbers with %s collatz steps exist%n", groupIndex, 36); | |
| System.out.printf("Smallest number: %s%n", smallestGroupNumber); | |
| System.out.printf("Biggest number: %s%n", biggestGroupNumber); | |
| } | |
| private static int collatz(int i) { | |
| if (i % 2 == 0) { | |
| return i / 2; | |
| } else { | |
| return 3 * i + 1; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CollatzNumbers { | |
| public static void main(String[] args) { | |
| for (int n = 101; n < 1000000; n++) { | |
| int stepIndex = 0; | |
| int step = n; | |
| while (stepIndex < 13 && step != 1) { | |
| step = collatz(step); | |
| stepIndex++; | |
| } | |
| if (stepIndex < 12) { | |
| System.out.printf("n: %s, steps: %s%n", n, stepIndex); | |
| } | |
| } | |
| } | |
| private static int collatz(int i) { | |
| if (i % 2 == 0) { | |
| return i / 2; | |
| } else { | |
| return 3 * i + 1; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment