Skip to content

Instantly share code, notes, and snippets.

@alcedo
Created August 26, 2013 10:59
Show Gist options
  • Save alcedo/6340309 to your computer and use it in GitHub Desktop.
Save alcedo/6340309 to your computer and use it in GitHub Desktop.
3n+1
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.System.out;
/**
* mistake: never read question properly. we need to calculate i upto and including j.
* hence for(; i<= j; i++) not for(; i< j; i++)
* the latter is done out of habbit =/
*/
public class Main {
//Start 1236
//+ 15 mins to reading questions
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int i = in.nextInt();
int j = in.nextInt();
int _i = i;
int _j = j;
int tmp;
if( i > j ) {
tmp = i;
i = j;
j = tmp;
}
int max = 0;
for(; i<=j; i++) {
int count = 0;
int n = i;
while(n != 1) {
//out.print(n + "," );
if(n % 2 != 0 )
n = 3*n+1;
else
n = n/2;
count++;
max = Math.max(max, count);
}
}
//System.out.println("");
System.out.println(_i + " " + _j + " " + (max+1));
//System.out.println("\n------------");
}
}//end of void main
}//end of class main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment