Created
April 30, 2012 01:42
-
-
Save them0nk/2554785 to your computer and use it in GitHub Desktop.
3n+1 Problem: PC/UVa IDs: 110101/100
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
/* | |
* Main.java | |
* java program model for www.programming-challenges.com | |
*/ | |
import java.io.*; | |
import java.util.*; | |
class hailstorm implements Runnable{ | |
public static void main(String args[]) // entry point from OS | |
{ | |
hailstorm myWork = new hailstorm(); // Construct the bootloader | |
myWork.run(); // execute | |
} | |
public void run() { | |
new myStuff().run(); | |
} | |
} | |
class myStuff implements Runnable{ | |
int maxSequenceLength(long start, long end) | |
{ | |
if (start > end) | |
{ | |
long temp; | |
temp = start; | |
start = end; | |
end = temp; | |
} | |
int max_count = 0; | |
for (long i=start; i <= end; i++) | |
{ | |
long num = i; | |
long count = 1; | |
while (num != 1) | |
{ | |
if (num%2 == 0) | |
num = num/2; | |
else | |
num = 3*num+1; | |
count++; | |
} | |
if (count > max_count) | |
max_count = (int)count; | |
} | |
return max_count; | |
}; | |
public void run(){ | |
try | |
{ | |
BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); | |
String inputLine; | |
while ((inputLine = is.readLine( )) != null) { | |
String a[] = inputLine.trim().split("\\s+"); | |
System.out.printf("%d %d %d\n",Integer.parseInt(a[0]),Integer.parseInt(a[1]), | |
maxSequenceLength(Integer.parseInt(a[0]),Integer.parseInt(a[1]))); | |
} | |
is.close( ); | |
} catch (Exception e) { | |
// System.out.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment