-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollatzConjecture.java
More file actions
39 lines (38 loc) · 978 Bytes
/
CollatzConjecture.java
File metadata and controls
39 lines (38 loc) · 978 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
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
public class CollatzConjecture {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for (int i = 0; i < t; i++) {
int x=sc.nextInt();
int y=sc.nextInt();
int k=sc.nextInt();
while(k>0){
if(x==1){
int a=y-x;
int r=k%a;
x=r+1;
break;
}
if(x==y){
x++;
k--;
}
int a=x%y;
a=y-a;
if(k-a>=0) {
k -= a;
x += a;
}
else{
x=x+k;
break;
}
while(x%y==0){
x=x/y;
}
}
System.out.println(x);
}
}
}