FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java_Programs/GeneratePrimes.java at main · prittoruban/Java_Programs · GitHub
prittoruban
/
Java_Programs
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java_Programs
/
GeneratePrimes.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
55 lines (50 loc) · 1.51 KB
Breadcrumbs
Java_Programs
/
GeneratePrimes.java
Copy path
File metadata and controls
55 lines (50 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
Scanner
;
class
PrimeGenerator
implements
Runnable
{
private
final
int
start
;
private
final
int
end
;
PrimeGenerator
(
int
start
,
int
end
) {
this
.
start
=
start
;
this
.
end
=
end
;
}
@
Override
public
void
run
() {
for
(
int
i
=
start
;
i
<=
end
;
i
++) {
if
(
isPrime
(
i
)) {
System
.
out
.
println
(
i
);
}
}
}
private
boolean
isPrime
(
int
num
) {
if
(
num
<=
1
)
return
false
;
for
(
int
i
=
2
;
i
*
i
<=
num
;
i
++) {
if
(
num
%
i
==
0
)
return
false
;
}
return
true
;
}
}
public
class
GeneratePrimes
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
try
(
Scanner
sc
=
new
Scanner
(
System
.
in
)) {
int
n
=
sc
.
nextInt
();
int
threads
=
sc
.
nextInt
();
List
<
Thread
>
threadList
=
new
ArrayList
<>();
int
start
=
1
;
int
end
=
n
/
threads
;
for
(
int
i
=
0
;
i
<
threads
;
i
++) {
if
(
i
==
threads
-
1
) {
end
=
n
;
}
Thread
thread
=
new
Thread
(
new
PrimeGenerator
(
start
,
end
));
threadList
.
add
(
thread
);
thread
.
start
();
start
=
end
+
1
;
end
+=
n
/
threads
;
}
for
(
Thread
thread
:
threadList
) {
thread
.
join
();
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL