100 - The 3n + 1 problem

Problem Link

import java.util.*;

public class Main {
public static void main(String[] args) {

int i =0,j =0,x;

Scanner scn = new Scanner(System.in);
while(scn.hasNextInt()) {
i = scn.nextInt();
j = scn.nextInt();


int start,end;
if(i > j) {
start = j;
end = i;
}
else {
start = i;
end = j;
}

int max = 0;
int n =0;
int count;
for(x=start;x<=end;x++) {
n = x;
count = 1;
while(n != 1) {
if(n%2 == 0)
n = n/2;
else
n = 3*n+1;
count++;
}

if(count > max )
max = count;
}
System.out.println(i +" " +j +" " +max);

}
}

}

Share this

Related Posts

Previous
Next Post »