Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created May 6, 2015 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustyconover/5dd31cae6e2acb2d96f8 to your computer and use it in GitHub Desktop.
Save rustyconover/5dd31cae6e2acb2d96f8 to your computer and use it in GitHub Desktop.
Collate sequence length in Java
public class Collatz {
private static int count_until_1(long n) {
int count = 0;
while(n != 1) {
if(n % 2 == 0) {
n /= 2;
} else {
n = (n * 3) + 1;
}
count++;
}
return count;
}
public static void main(String[] args){
long i, length, number;
length = 0;
number = 0;
for(i = 1; i < 300000; i++) {
long l = count_until_1(i);
if(length < l) {
length = l;
number = i;
}
}
System.out.println("Maximum stopping distance " + length + ", starting number " + number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment