loop 3n plus 1 problem

package Chapter 2;


import java.util.Scanner;
//int 32-bit integer
/*
 * Conjecture: For any natural number n greater than one, if n is odd, change n to 3n+1, otherwise it becomes n Half through several transformations must make n become 1.
 * For example,
 * 3->10->5->16->8->4->2->1
 * input n, output the number of transformations. n<=10 to the ninth power
 * Sample input:
 * 3
 * Sample output:
 * 7
 * */
public class loop 3n plus 1 problem {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
//Pay attention to the size of the data type
long n= in.nextLong();
int count=0;
while(n>1) {
if(n%2==0) {
n=n/2;
}
else {
n=3*n+1;
}
count++;
}
System.out.println(count);
}


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860572&siteId=291194637