-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathHailstone.java
More file actions
62 lines (49 loc) · 1.37 KB
/
Hailstone.java
File metadata and controls
62 lines (49 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
public class Hailstone {
public static int count=0; // static variable (class variable)
public static String Seq="";
//class method 1 : cheching if number is even
public static boolean isEven(int n){
return n%2==0;
}
//class method 2 : generating sequence
public static void hail(int n){
System.out.println(n);
if (n==1){
return;
}
else if (isEven(n)){
count++;
hail(n/2);
}
else{
count++;
hail(3*n+1);
}
}
public static String hailstone_Str(int n){
while (n>1){
Seq+=String.valueOf(n)+", ";
if (isEven(n)){
n=n/2;
}
else{
n=3*n+1;
}
}
Seq+="1";
return Seq;
}
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("enter number:");
int num= in.nextInt();
int num_ini=num;
System.out.println("Hail stone sequence for "+num+" is;");
hail(num);
System.out.println("it took "+Hailstone.count+" moves to go from "+num_ini+" to 1");
in.close();
String seq_out=hailstone_Str(num);
System.out.println(seq_out);
}
}