-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP5_9.java
More file actions
56 lines (50 loc) · 1.38 KB
/
P5_9.java
File metadata and controls
56 lines (50 loc) · 1.38 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class P5_9 {
public static void main(String[] args) {
System.out.println(primes(100000).toString());
}
public static List<Integer> primes(int n)
{
//With solution we waste time by computing if certain numbers are prime multiple times.
//Better approach is to keep a boolean of primes that contains the multiples of each prime number up to n.
//That solution checks odd numbers also. My solution seems pretty good.
// List<Integer> primes = new ArrayList<Integer>();
// if(n < 0) {return primes;}
// primes.add(2);
// outer: for(int i = 3;i <= n; i+=2)
// {
// for(int p : primes)
// {
// if(p > Math.sqrt(i)) {break;}
// if(i % p == 0)
// continue outer;
// }
// primes.add(i);
// }
//
// if(n < 3) {return 0;}
// int count = 1;
//More efficient solutin
//ADD CHECK FOR N < 2
List<Integer> primes = new ArrayList<Integer>();
primes.add(2);
List<Boolean> isPrime = new ArrayList<>(Collections.nCopies(n +1, true));
isPrime.set(0, false);
isPrime.set(1, false);
isPrime.set(2, true);
for(int i = 3;i < n; i+=2)
{
if(isPrime.get(i))
{
primes.add(i);
for(int p = 2 * i; p < n; p += i)
{
isPrime.set(p, false);
}
}
}
return primes;
}
}