-
Notifications
You must be signed in to change notification settings - Fork 0
/
Juggler sequence
46 lines (35 loc) · 1.1 KB
/
Juggler sequence
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
//Juggler sequence
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
long n = Long.parseLong(in.readLine());
Solution ob = new Solution();
List<Long> ans = new ArrayList<>();
StringBuilder out = new StringBuilder();
ans = ob.jugglerSequence(n);
for(int i = 0; i < ans.size(); i++)
out.append(ans.get(i) + " ");
System.out.println(out);
}
}
}
class Solution {
static List<Long> jugglerSequence(long n) {
List<Long> ans = new ArrayList<Long>();
while(n != 1) {
ans.add(n);
if(n % 2 == 1) {
n = (long) Math.pow(Math.sqrt(n), 3);
}
else {
n = (long) Math.sqrt(n);
}
}
ans.add(1L);
return ans;
}
}