Created
February 9, 2009 02:25
-
-
Save skalnik/60617 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.*; | |
public class pr14 | |
{ | |
static HashMap<Integer, Integer> mappy; | |
public static void main(String[] args) | |
{ | |
mappy = new HashMap<Integer, Integer>(); | |
for(int i=2; i<1000000; ++i) | |
{ | |
int l = length(i); | |
mappy.put(i, l); | |
System.out.println(i + " " + l); | |
} | |
Set<Integer> setty = mappy.keySet(); | |
Iterator iter = setty.iterator(); | |
int maxl = 0, maxs = 0; | |
while(iter.hasNext()) | |
{ | |
int curs = ((Integer)iter.next()).intValue(); | |
int curl = mappy.get(curs); | |
if(curl > maxl) | |
{ | |
maxl = curl; | |
maxs = curs; | |
} | |
} | |
System.out.println("Start: " + maxs + "\nLength: " + maxl); | |
} | |
public static int length(int start) | |
{ | |
int length = 0; | |
long current = (long)start; | |
while(current != 1) | |
{ | |
if(mappy.containsKey(current)) | |
{ | |
current = 1; | |
length = mappy.get(current); | |
break; | |
} | |
else if(current % 2 == 0) | |
current >>= 1; | |
else | |
{ | |
current *= 3; | |
current++; | |
} | |
length++; | |
} | |
return length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment