forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
41 lines (33 loc) · 1.09 KB
/
solution.java
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
import java.util.Scanner;
public class Solution {
static final int LIMIT = 1_000_000;
static int[] solutions = buildSolutions();
// main function
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Q = sc.nextInt(); // number of queries
for (int i = 0; i < Q; i++) {
int N = sc.nextInt(); // single number
System.out.println(solve(N)); // calling function solve()
}
sc.close();
}
static int[] buildSolutions() {
int[] solutions = new int[LIMIT + 1];
for (int i = 1; i < solutions.length; i++) {
solutions[i] = solutions[i - 1] + 1;
// Check if it is a prime number or not
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
solutions[i] = Math.min(solutions[i], solutions[i / j] + 1); // get minimum
}
}
}
// returning array solutions
return solutions;
}
// solve function
static int solve(int N) {
return solutions[N];
}
}