File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
src/main/java/main/java/videos Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ package main .java .videos ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ public class PrimeNumberSieve {
7+
8+ public static void main (String [] args ) {
9+ System .out .println (findAllPrimesUpto (100 ));
10+ }
11+
12+ public static List <Integer > findAllPrimesUpto (final int n ) {
13+ final boolean [] touched = new boolean [n + 1 ];
14+ final double sqrtOfN = Math .sqrt (n );
15+ final List <Integer > primes = new ArrayList <>();
16+ primes .add (2 );
17+ for (int i = 2 ; i < n ; i += 2 ) {
18+ touched [i ] = true ;
19+ }
20+ for (int i = 3 ; i <= sqrtOfN ; i ++) {
21+ if (!touched [i ]) {
22+ for (int j = i * i ; j <= n ; j = j + (i * 2 )) {
23+ touched [j ] = true ;
24+ }
25+ }
26+ }
27+ for (int i = 2 ; i <= n ; i ++) {
28+ if (!touched [i ]) {
29+ primes .add (i );
30+ }
31+ }
32+ return primes ;
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments