Created
October 12, 2011 04:31
-
-
Save ellbur/1280279 to your computer and use it in GitHub Desktop.
Collatz puzzle
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 { | |
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