Skip to content

Instantly share code, notes, and snippets.

@timble-one
Created May 23, 2020 11:51
Show Gist options
  • Select an option

  • Save timble-one/2b33ed0c3a386604a98ab3672275edc9 to your computer and use it in GitHub Desktop.

Select an option

Save timble-one/2b33ed0c3a386604a98ab3672275edc9 to your computer and use it in GitHub Desktop.
Gollatz Timo
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;
}
}
}
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