Created
May 6, 2015 02:11
-
-
Save rustyconover/5dd31cae6e2acb2d96f8 to your computer and use it in GitHub Desktop.
Collate sequence length in Java
This file contains 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 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