Skip to content

Commit 224d300

Browse files
committed
java PrimeNumbers
1 parent b7c97e1 commit 224d300

File tree

1 file changed

+122
-13
lines changed

1 file changed

+122
-13
lines changed

algorithms/PrimeNumbers.java

Lines changed: 122 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,125 @@
11
package algorithms;
22

3+
import java.util.Arrays;
4+
35
public class PrimeNumbers {
4-
5-
public static void print(int cnt) {
6-
int cur_cnt = 0;
7-
while(cur_cnt <= cnt) {
8-
//l
9-
}
10-
} //print
11-
12-
public static void main(String[] args) {
13-
print(20);
14-
} //main
15-
16-
} //PrimeNumbers
6+
7+
public static void print_n2(int max_num) {
8+
int cnt_iterat = 0;
9+
boolean divided = false;
10+
11+
for(int i = 1; i<= max_num; i++) {
12+
divided = false;
13+
for(int j = 2; j < i; j++) {
14+
cnt_iterat++;
15+
if( i%j == 0 ) {
16+
divided = true;
17+
break;
18+
} //if
19+
} //for
20+
21+
if(!divided) {
22+
System.out.print(i + " ");
23+
}
24+
} //for
25+
System.out.println("(" +cnt_iterat+ ")");
26+
} //print_n2
27+
28+
public static void print_sqrt(int max_num) {
29+
int cnt_iterat = 0;
30+
boolean divided = false;
31+
32+
for(int i = 1; i<= max_num; i++) {
33+
34+
if( i > 2 && i%2 == 0 ) {
35+
cnt_iterat++;
36+
continue; //пропустим четные
37+
}
38+
39+
divided = false;
40+
41+
for(int j = 3; j < Math.sqrt(i) + 1; j = j + 2) {
42+
cnt_iterat++;
43+
44+
if( i%j == 0 ) {
45+
divided = true;
46+
break;
47+
} //if
48+
} //for
49+
50+
if(!divided) {
51+
System.out.print(i + " ");
52+
}
53+
} //for
54+
System.out.println("(" +cnt_iterat+ ")");
55+
} //print_sqrt
56+
57+
// Первое вычеркивание требует n/2 действий, второе — n/3, третье — n/5 и т. д.
58+
//O(N*log(log(N))),
59+
//память O(n)
60+
public static void print_eratosfen(int max_num) {
61+
int cnt_iterat = 0;
62+
63+
boolean isPrime[] = new boolean[max_num + 1];
64+
Arrays.fill(isPrime,true);
65+
66+
//все должно делиться на числа до корня
67+
for (int i=2; i < Math.sqrt(max_num) + 1; i++) {
68+
if (isPrime[i]) {
69+
//помечаем числа кратные простому
70+
for (int j=i*i; j <= max_num; j+=i) {
71+
isPrime[j] = false;
72+
cnt_iterat++;
73+
}
74+
} else {
75+
cnt_iterat++;
76+
}
77+
78+
}
79+
System.out.print("1 2 ");
80+
//довыводим массив с корня до конца
81+
for (int i=3; i <= max_num; i = i + 2) {
82+
if(isPrime[i]) System.out.print(i + " ");
83+
cnt_iterat++;
84+
}
85+
System.out.println("(" +cnt_iterat+ ")");
86+
} //print_eratosfen
87+
88+
//оптимизация без четных чисел. Память = O(n)/2; Сложность = O(N*log(log(N)))/2
89+
public static void print_eratosfen2(int max_num) {
90+
int cnt_iterat = 0;
91+
92+
boolean isPrime[] = new boolean[max_num/2 + 1];
93+
Arrays.fill(isPrime,true);
94+
95+
//все должно делиться на числа до корня
96+
for (int i=3; i < Math.sqrt(max_num) + 1; i = i + 2) {
97+
if (isPrime[(i-1)/2]) {
98+
//помечаем числа кратные простому
99+
for (int j=i*i; j <= max_num; j+=i) {
100+
isPrime[(j-1)/2] = false;
101+
cnt_iterat++;
102+
}
103+
} else {
104+
cnt_iterat++;
105+
}
106+
107+
}
108+
//довыводим массив с корня до конца
109+
//1 3 5 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 101
110+
for (int i=0; i < isPrime.length; i++) {
111+
//if(isPrime[i]) System.out.print(i*2+1 + " ");
112+
if(isPrime[i]) System.out.print(i + " ");
113+
cnt_iterat++;
114+
}
115+
System.out.println("(" +cnt_iterat+ ")");
116+
} //print_eratosfen2
117+
118+
public static void main(String[] args) {
119+
print_n2(1000);
120+
print_sqrt(1000);
121+
print_eratosfen(1000);
122+
print_eratosfen2(100);
123+
}
124+
125+
}

0 commit comments

Comments
 (0)