Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created August 10, 2012 00:30
Show Gist options
  • Save JossWhittle/3309527 to your computer and use it in GitHub Desktop.
Save JossWhittle/3309527 to your computer and use it in GitHub Desktop.
public void p14() {
int max = 0;
for (int i = 1000000; i > 0; i--) {
int c = collatz((long)i,2);
if (c > max) {
max = c;
}
}
System.out.println(max);
}
public int collatz(long n, int c) {
if (n % 2L == 0L) {
n = n / 2L;
} else {
n = (3L * n) + 1L;
}
if (n == 1) {
return c;
}
return collatz(n, c + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment