Skip to content

Instantly share code, notes, and snippets.

@skalnik
Created February 9, 2009 02:25
Show Gist options
  • Save skalnik/60617 to your computer and use it in GitHub Desktop.
Save skalnik/60617 to your computer and use it in GitHub Desktop.
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