Commit 60178602 authored by Ali Saeed's avatar Ali Saeed

Modify Main.java file to test the required time to find prime numbers in range...

Modify Main.java file to test the required time to find prime numbers in range it's length is 25000000
parent 16ecd78b
...@@ -5,13 +5,9 @@ ...@@ -5,13 +5,9 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="b4d948cc-57e4-454c-8341-c678ef0590f0" name="Changes" comment=""> <list default="true" id="b4d948cc-57e4-454c-8341-c678ef0590f0" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Report.docx" beforeDir="false" afterPath="$PROJECT_DIR$/Report.docx" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/org/example/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/example/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Main.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
...@@ -38,15 +34,15 @@ ...@@ -38,15 +34,15 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrary": "JUnit3", &quot;com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrary&quot;: &quot;JUnit3&quot;,
"com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrarySuperClass.JUnit3": "junit.framework.TestCase" &quot;com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrarySuperClass.JUnit3&quot;: &quot;junit.framework.TestCase&quot;
} }
}]]></component> }</component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CreateTestDialog.Recents.Supers"> <key name="CreateTestDialog.Recents.Supers">
<recent name="junit.framework.TestCase" /> <recent name="junit.framework.TestCase" />
......
...@@ -5,17 +5,30 @@ import java.util.List; ...@@ -5,17 +5,30 @@ import java.util.List;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("the first case: normal case with 4 threads");
FindPrimeNumbersWithMultiThreading(1,100000000,4);
System.out.println("the second case: increase the number of threads to 5 without change the search range ");
FindPrimeNumbersWithMultiThreading(1,100000000,5);
System.out.println("the second case: increase the number of threads to 10 without change the search range ");
FindPrimeNumbersWithMultiThreading(1,100000000,10);
System.out.println("the second case: increase the number of threads to 20 without change the search range ");
FindPrimeNumbersWithMultiThreading(1,100000000,20);
System.out.println("the number of prime numbers on range 1 to 25000000 using one thread");
FindPrimeNumbersWithMultiThreading(1,25000000,1);
System.out.println("the number of prime numbers on range 25000000 to 50000000 using one thread ");
FindPrimeNumbersWithMultiThreading(25000000,50000000,1);
System.out.println("the number of prime numbers on range 50000000 to 75000000 using one thread");
FindPrimeNumbersWithMultiThreading(50000000,75000000,1);
System.out.println("the number of prime numbers on range 75000000 to 100000000 using one thread");
FindPrimeNumbersWithMultiThreading(75000000,100000000,1);
// System.out.println("the second case: increase the number of threads to 5 without change the search range ");
// FindPrimeNumbersWithMultiThreading(1,100000000,5);
// System.out.println("the second case: increase the number of threads to 10 without change the search range ");
//
// FindPrimeNumbersWithMultiThreading(1,100000000,10);
// System.out.println("the second case: increase the number of threads to 20 without change the search range ");
//
// FindPrimeNumbersWithMultiThreading(1,100000000,20);
// System.out.println("the third case: change search range to 50000000 without increase the number of threads"); // System.out.println("the third case: change search range to 50000000 without increase the number of threads");
// FindPrimeNumbersWithMultiThreading(0,50000000,4); // FindPrimeNumbersWithMultiThreading(0,50000000,4);
...@@ -26,12 +39,17 @@ public class Main { ...@@ -26,12 +39,17 @@ public class Main {
} }
static public void FindPrimeNumbersWithMultiThreading(int a, int b , int threadNumbers){ static public void FindPrimeNumbersWithMultiThreading(int a, int b , int threadNumbers){
List<Thread> threads = new ArrayList<>(); List<Thread> threads = new ArrayList<>();
int subRange = ((b -a)+1)/threadNumbers; int subRangeSize = ((b -a)+1)/threadNumbers;
int remainingNumbers = (b - a + 1 )%threadNumbers;
ArrayList<PrimeNumbersService> servers = new ArrayList<>(); ArrayList<PrimeNumbersService> servers = new ArrayList<>();
for (int i=0 ; i < threadNumbers; i++){ for (int i=0 ; i < threadNumbers; i++){
int start = i*subRange + 1; int start = a + (i*subRangeSize);
int end = subRange*(i+1); int end = a + subRangeSize -1;
if (i == threadNumbers - 1) {
end += remainingNumbers; // Add remaining numbers to the last segment
}
PrimeNumbersService x = new PrimeNumbersService(start , end); PrimeNumbersService x = new PrimeNumbersService(start , end);
Thread thread = new Thread(x); Thread thread = new Thread(x);
threads.add(thread); threads.add(thread);
...@@ -55,4 +73,5 @@ public class Main { ...@@ -55,4 +73,5 @@ public class Main {
System.out.println("Total prime numbers found: " + allPrimeNumbers.size()); System.out.println("Total prime numbers found: " + allPrimeNumbers.size());
System.out.println("Execution time: " + (endTime - startTime) + " milliseconds"); System.out.println("Execution time: " + (endTime - startTime) + " milliseconds");
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment