-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatz.java
More file actions
29 lines (24 loc) · 745 Bytes
/
Collatz.java
File metadata and controls
29 lines (24 loc) · 745 Bytes
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
import java.util.Scanner;
public class Collatz {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t>0) {
int k = sc.nextInt();
int initial = sc.nextInt();
for (int i = 0; i < k; i++) {
if (initial % 2 == 0) {
initial =initial* 2;
} else {
if((initial-1) %3 == 0 && ((initial -1)/3) %2 ==1){
initial = (initial - 1) / 3;
}else{
initial = initial*2;
}
}
}
System.out.println(initial);
t--;
}
}
}