Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
Abdullah_Alsoleman1
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
abdullh.alsoleman
Abdullah_Alsoleman1
Commits
5327a8d9
Commit
5327a8d9
authored
Oct 31, 2023
by
abdullh.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add main
parent
f100d128
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
146 additions
and
0 deletions
+146
-0
Main.java
src/main/java/Main.java
+49
-0
PrimeNumberService.java
src/main/java/PrimeNumberService.java
+66
-0
Main.java
src/main/java/org/example/Main.java
+7
-0
PrimeNumberServiceTest.java
src/test/java/PrimeNumberServiceTest.java
+24
-0
No files found.
src/main/java/Main.java
0 → 100644
View file @
5327a8d9
import
java.util.ArrayList
;
import
java.util.List
;
//Abdullah Alsoleman
public
class
Main
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
int
startRange
=
1
;
int
endRange
=
100_000_000
;
int
numThreads
=
4
;
int
chunkSize
=
(
endRange
-
startRange
+
1
)
/
numThreads
;
List
<
Thread
>
threads
=
new
ArrayList
<>();
List
<
PrimeNumberService
>
allChunks_PrimeNumbers
=
new
ArrayList
<
PrimeNumberService
>();
for
(
int
i
=
0
;
i
<
numThreads
;
i
++)
{
int
start
=
startRange
+
i
*
chunkSize
;
int
end
=
start
+
chunkSize
-
1
;
if
(
i
==
numThreads
-
1
)
{
end
=
endRange
;
}
PrimeNumberService
chunk_primeNumber
=
new
PrimeNumberService
(
start
,
end
);
Thread
thread
=
new
Thread
(
chunk_primeNumber
);
threads
.
add
(
thread
);
allChunks_PrimeNumbers
.
add
(
chunk_primeNumber
);
thread
.
start
();
}
long
startTime
=
System
.
currentTimeMillis
();
for
(
Thread
thread
:
threads
)
{
thread
.
join
();
}
long
endTime
=
System
.
currentTimeMillis
();
List
<
Integer
>
allPrimeNumbers
=
new
ArrayList
<>();
for
(
PrimeNumberService
primeNumbers
:
allChunks_PrimeNumbers
)
{
allPrimeNumbers
.
addAll
(
primeNumbers
.
getPrimeNumbers
());
}
System
.
out
.
println
(
"Total prime numbers found: "
+
allPrimeNumbers
.
size
());
System
.
out
.
println
(
"Execution time: "
+
(
endTime
-
startTime
)
+
" milliseconds"
);
}
}
src/main/java/PrimeNumberService.java
0 → 100644
View file @
5327a8d9
import
java.util.ArrayList
;
import
java.util.List
;
public
class
PrimeNumberService
implements
Runnable
{
private
int
start
;
private
int
end
;
private
List
<
Integer
>
primeNumbers
;
public
PrimeNumberService
()
{
}
public
PrimeNumberService
(
int
start
,
int
end
)
{
this
.
start
=
start
;
this
.
end
=
end
;
primeNumbers
=
new
ArrayList
<
Integer
>();
}
public
int
getStart
()
{
return
start
;
}
public
int
getEnd
()
{
return
end
;
}
public
List
<
Integer
>
getPrimeNumbers
()
{
return
primeNumbers
;
}
public
void
setStart
(
int
start
)
{
this
.
start
=
start
;
}
public
void
setEnd
(
int
end
)
{
this
.
end
=
end
;
}
public
boolean
isPrime
(
int
num
){
if
(
num
<=
1
)
{
return
false
;
}
for
(
int
i
=
2
;
i
<=
Math
.
sqrt
(
num
);
i
++)
{
if
(
num
%
i
==
0
)
{
return
false
;
}
}
return
true
;
}
public
List
<
Integer
>
FindPrimeNumbersInRange
(){
List
<
Integer
>
range_primeNumbers
=
new
ArrayList
<>();
for
(
int
num
=
start
;
num
<=
end
;
num
++)
{
if
(
isPrime
(
num
))
{
range_primeNumbers
.
add
(
num
);
}
}
return
range_primeNumbers
;
}
@Override
public
void
run
()
{
primeNumbers
.
addAll
(
FindPrimeNumbersInRange
());
}
}
src/main/java/org/example/Main.java
0 → 100644
View file @
5327a8d9
package
org
.
example
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello world!"
);
}
}
\ No newline at end of file
src/test/java/PrimeNumberServiceTest.java
0 → 100644
View file @
5327a8d9
import
junit.framework.TestCase
;
import
org.junit.Test
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
PrimeNumberServiceTest
extends
TestCase
{
@Test
public
void
testIsPrime
()
{
PrimeNumberService
test
=
new
PrimeNumberService
();
assertTrue
(
test
.
isPrime
(
7
));
}
@Test
public
void
testFindPrimeNumbersInRange
(){
PrimeNumberService
test
=
new
PrimeNumberService
(
1
,
20
);
List
<
Integer
>
expected
=
Arrays
.
asList
(
2
,
3
,
5
,
7
,
11
,
13
,
17
,
19
);
assertEquals
(
expected
,
test
.
FindPrimeNumbersInRange
());
}
}
\ 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