-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeastPrimeFactor.java
29 lines (24 loc) · 1.04 KB
/
LeastPrimeFactor.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
import java.util.Arrays;
public class LeastPrimeFactor {
public static void main(String[] args) {
int N = 20; // Change N to the desired number
int[] leastPrimeFactors = new int[N + 1];
Arrays.fill(leastPrimeFactors, -1); // Initialize with -1 indicating prime
for (int i = 2; i <= N; i++) {
if (leastPrimeFactors[i] == -1) { // i is prime
leastPrimeFactors[i] = i; // Least prime factor of a prime number is the number itself
// Mark multiples of i with i as their least prime factor
for (int j = i * 2; j <= N; j += i) {
if (leastPrimeFactors[j] == -1) {
leastPrimeFactors[j] = i;
}
}
}
}
// Print the least prime factors for all numbers from 1 to N
System.out.println("Least Prime Factors:");
for (int i = 1; i <= N; i++) {
System.out.println(i + " : " + leastPrimeFactors[i]);
}
}
}