Skip to content

Instantly share code, notes, and snippets.

@ellbur
Created October 12, 2011 04:31
Show Gist options
  • Save ellbur/1280279 to your computer and use it in GitHub Desktop.
Save ellbur/1280279 to your computer and use it in GitHub Desktop.
Collatz puzzle
public class Collatz {
public static void main(String[] args) {
int n;
int count = 0;
int highest = 0;
int k = 1;
for ( int i = 1; i<=100 ; i++) {
n = i;
count = 0;
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
//System.out.println(n);
}
else {
n = n * 3 +1;
//System.out.println(n);
}
count++;
}
if (count > highest) {
highest = count;
k = i;
}
//System.out.println(count);
}
System.out.println(k);
System.out.println(highest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment