-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumbers.java
More file actions
69 lines (53 loc) · 2.16 KB
/
Copy pathPrimeNumbers.java
File metadata and controls
69 lines (53 loc) · 2.16 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package algorithms.classics;
import java.util.*;
import java.util.logging.*;
/*
https://www.geeksforgeeks.org/java/java-prime-number-program/
* Prime Numbers: Individual verification + Sieve of Eratosthenes (algorithm to find all primes up to N)
* Single check complexity: O(√n)
* Sieve of Eratosthenes complexity: O(n log log n)
*/
public class PrimeNumbers {
private static final Logger LOGGER = Logger.getLogger(PrimeNumbers.class.getName());
// Checks if a single number is prime
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; (long) i * i <= n; i++) { // Only check up to square root
if (n % i == 0) {
return false;
}
}
return true;
}
// Sieve of Eratosthenes: Efficiently finds all prime numbers up to 'limit'
public static List<Integer> sieveOfEratosthenes(int limit) {
// If the limit is less than 2, return an empty list since there are no primes below 2
if (limit < 2) {
return List.of();
}
// Create a boolean array to mark composite numbers. Initially, all numbers are assumed to be prime (false).
boolean[] isComposite = new boolean[limit + 1];
List<Integer> primes = new ArrayList<>();
// Iterate through numbers starting from 2 up to the limit
for (int i = 2; i <= limit; i++) {
if (!isComposite[i]) {
primes.add(i);
// Mark all multiples of i as composite (not prime)
for (long j = (long) i * i; j <= limit; j += i) {
isComposite[(int) j] = true;
}
}
}
return primes;
}
public static void main() {
// Check if a specific number is prime with the method isPrime
int number = 29;
LOGGER.log(Level.INFO, "Is {0} prime?: {1}", new Object[]{number, isPrime(number)});
// Find all prime numbers up to a certain limit using the Sieve of Eratosthenes
int limit = 50;
LOGGER.log(Level.INFO, "Primes up to {0}: {1}", new Object[]{limit, sieveOfEratosthenes(limit)});
}
}