forked from anishrauniyar/EasySolutionMUM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuthrieSequence.java
More file actions
82 lines (72 loc) · 1.8 KB
/
GuthrieSequence.java
File metadata and controls
82 lines (72 loc) · 1.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
* Start with a positive number n
if n is even then divide by 2
if n is odd then multiply by 3 and add 1
continue this until n becomes 1
The Guthrie sequence of a positive number n is defined to be the numbers generated
by the above algorithm.
For example, the Guthrie sequence of the number 7 is
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
*/
public class GuthrieSequence {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the values for Guthrie Sequence: ");
List<Integer> value = new ArrayList<Integer>();
for (;;) {
int val = sc.nextInt();
value.add(val);
if (val == 1) {
break;
}
}
int test = isGuthrieSequence(value);
if (test == 1) {
System.out.println("The sequence is Guthrie Sequence.");
} else {
System.out.println("The sequence is not Guthrie Sequence.");
}
}
private static int isGuthrieSequence(List<Integer> value) {
// TODO Auto-generated method stub
int start = value.get(0);
int flag = 0;
List<Integer> actualSeries = new ArrayList<Integer>();
actualSeries.add(start);
if (start < 0) {
return 0;
} else {
for (;;) {
if (start % 2 == 0) {
start = start / 2;
actualSeries.add(start);
} else {
start = (start * 3) + 1;
actualSeries.add(start);
}
if (start == 1) {
flag++;
break;
}
}
if (flag == 1) {
flag = 0;
for (int i = 0; i < value.size(); i++) {
if (value.get(i) != actualSeries.get(i)) {
flag++;
break;
}
}
if (flag == 0) {
return 1;
} else {
return 0;
}
}
}
return 0;
}
}