Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
S
Sahar_moalla
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sahar.moalla
Sahar_moalla
Commits
f691f516
Commit
f691f516
authored
Oct 31, 2023
by
sahar.moalla
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add main.java
parents
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
117 additions
and
0 deletions
+117
-0
Main.java
src/main/java/Main.java
+39
-0
PrimeNumberGenerator.java
src/main/java/PrimeNumberGenerator.java
+61
-0
PrimeNumberGeneratorTest.java
src/test/java/PrimeNumberGeneratorTest.java
+17
-0
No files found.
src/main/java/Main.java
0 → 100644
View file @
f691f516
import
java.util.ArrayList
;
import
java.util.List
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
int
rangeStart
=
1
;
int
rangeEnd
=
100_000_000
;
int
threadsNumber
=
4
;
int
partialRange
=
(
rangeEnd
-
rangeStart
)/
threadsNumber
;
List
<
PrimeNumberGenerator
>
primeNumberGeneratorList
=
new
ArrayList
<
PrimeNumberGenerator
>();
List
<
Thread
>
threadList
=
new
ArrayList
<
Thread
>();
List
<
Integer
>
primeNumbers
=
new
ArrayList
<
Integer
>();
for
(
int
i
=
0
;
i
<
threadsNumber
;
i
++){
int
partialRangeStart
=
rangeStart
+
partialRange
*
i
;
int
partialRangeEnd
=
partialRangeStart
+
partialRange
-
1
;
PrimeNumberGenerator
primeNumberGenerator
=
new
PrimeNumberGenerator
(
partialRangeStart
,
partialRangeEnd
);
Thread
thread
=
new
Thread
(
primeNumberGenerator
);
threadList
.
add
(
thread
);
primeNumberGeneratorList
.
add
(
primeNumberGenerator
);
thread
.
start
();
}
long
startTime
=
System
.
currentTimeMillis
();
for
(
Thread
thread
:
threadList
)
{
thread
.
join
();
}
long
endTime
=
System
.
currentTimeMillis
();
for
(
PrimeNumberGenerator
primeNumberGenerator
:
primeNumberGeneratorList
)
{
primeNumbers
.
addAll
(
primeNumberGenerator
.
getGeneratedPrimeNumbers
());
}
System
.
out
.
println
(
"Number of prime numbers found: "
+
primeNumbers
.
size
());
System
.
out
.
println
(
"Execution time: "
+
(
endTime
-
startTime
)
+
" milliseconds"
);
}
}
src/main/java/PrimeNumberGenerator.java
0 → 100644
View file @
f691f516
import
java.util.ArrayList
;
import
java.util.List
;
public
class
PrimeNumberGenerator
implements
Runnable
{
public
PrimeNumberGenerator
(
int
rangeStart
,
int
rangeEnd
)
{
this
.
setRangeStart
(
rangeStart
);
this
.
setRangeEnd
(
rangeEnd
);
generatedPrimeNumbers
=
new
ArrayList
<
Integer
>();
}
public
PrimeNumberGenerator
()
{
}
public
int
getRangeStart
()
{
return
rangeStart
;
}
public
int
getRangeEnd
()
{
return
rangeEnd
;
}
public
void
setRangeStart
(
int
rangeStart
)
{
this
.
rangeStart
=
rangeStart
;
}
public
void
setRangeEnd
(
int
rangeEnd
)
{
this
.
rangeEnd
=
rangeEnd
;
}
public
List
<
Integer
>
getGeneratedPrimeNumbers
()
{
return
generatedPrimeNumbers
;
}
public
boolean
checkPrime
(
int
number
){
if
(
number
<=
1
)
return
false
;
else
if
(
number
==
2
)
return
true
;
else
{
for
(
int
i
=
2
;
i
<=
Math
.
sqrt
(
number
)
;
i
++){
if
(
number
%
i
==
0
){
return
false
;
}
}
return
true
;
}
}
private
int
rangeStart
;
private
int
rangeEnd
;
private
List
<
Integer
>
generatedPrimeNumbers
;
@Override
public
void
run
()
{
int
i
=
this
.
getRangeStart
();
for
(
;
i
<
this
.
getRangeEnd
()
;
i
++){
if
(
this
.
checkPrime
(
i
))
generatedPrimeNumbers
.
add
(
i
);
}
}
}
src/test/java/PrimeNumberGeneratorTest.java
0 → 100644
View file @
f691f516
import
junit.framework.TestCase
;
import
org.junit.Test
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
PrimeNumberGeneratorTest
extends
TestCase
{
@Test
public
void
testcheckPrime
()
{
PrimeNumberGenerator
primeNumberGenerator
=
new
PrimeNumberGenerator
();
assertTrue
(
primeNumberGenerator
.
checkPrime
(
23
));
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment