Commit 598b523d authored by mohammad.salama's avatar mohammad.salama

Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)

parent f698e009
...@@ -4,11 +4,12 @@ ...@@ -4,11 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="156428c6-b9a7-4d3a-9504-b3537029ac6a" name="Changes" comment="Creating Simple Finding Primes in Interval"> <list default="true" id="156428c6-b9a7-4d3a-9504-b3537029ac6a" name="Changes" comment="Finding Primes from 1 to 10^7 using multi-threading">
<change afterPath="$PROJECT_DIR$/src/main/java/SearchForPrimesThread.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/FibonacciSeries.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/SearchForPrimesThreadTest.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/FibonacciSeriesTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/SearchForPrimesThreadTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/SearchForPrimesThreadTest.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" />
...@@ -36,14 +37,34 @@ ...@@ -36,14 +37,34 @@
<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;,
"last_opened_file_path": "D:/HIAST/FIY/FS/Parallel Programming/Lab/1/PP-lec01-Threads" &quot;last_opened_file_path&quot;: &quot;D:/HIAST/FIY/FS/Parallel Programming/Lab/1/PP-lec01-Threads&quot;
} }
}]]></component> }</component>
<component name="RunManager" selected="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest"> <component name="RunManager" selected="JUnit.FibonacciSeriesTest.testFibonacciSeriesRec">
<configuration name="FibonacciSeriesTest.testFibonacciSeriesIter" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="FibonacciSeriesTest" />
<option name="METHOD_NAME" value="testFibonacciSeriesIter" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="FibonacciSeriesTest.testFibonacciSeriesRec" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="FibonacciSeriesTest" />
<option name="METHOD_NAME" value="testFibonacciSeriesRec" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="FindPrimesTest.testFindPrimes" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="FindPrimesTest.testFindPrimes" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" /> <module name="Lab101" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
...@@ -75,6 +96,8 @@ ...@@ -75,6 +96,8 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="JUnit.FibonacciSeriesTest.testFibonacciSeriesRec" />
<item itemvalue="JUnit.FibonacciSeriesTest.testFibonacciSeriesIter" />
<item itemvalue="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest" /> <item itemvalue="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest" />
<item itemvalue="JUnit.SearchForPrimesThreadTest" /> <item itemvalue="JUnit.SearchForPrimesThreadTest" />
<item itemvalue="JUnit.FindPrimesTest.testFindPrimes" /> <item itemvalue="JUnit.FindPrimesTest.testFindPrimes" />
...@@ -104,7 +127,14 @@ ...@@ -104,7 +127,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1698436032981</updated> <updated>1698436032981</updated>
</task> </task>
<option name="localTasksCounter" value="3" /> <task id="LOCAL-00003" summary="Finding Primes from 1 to 10^7 using multi-threading">
<created>1698439374948</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1698439374948</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
...@@ -121,6 +151,7 @@ ...@@ -121,6 +151,7 @@
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="first" /> <MESSAGE value="first" />
<MESSAGE value="Creating Simple Finding Primes in Interval" /> <MESSAGE value="Creating Simple Finding Primes in Interval" />
<option name="LAST_COMMIT_MESSAGE" value="Creating Simple Finding Primes in Interval" /> <MESSAGE value="Finding Primes from 1 to 10^7 using multi-threading" />
<option name="LAST_COMMIT_MESSAGE" value="Finding Primes from 1 to 10^7 using multi-threading" />
</component> </component>
</project> </project>
\ No newline at end of file
import java.util.ArrayList;
import java.util.List;
public class FibonacciSeries
{
private List<Long> Fibs;
public FibonacciSeries()
{
Fibs = new ArrayList<>();
Fibs.add(0L);
Fibs.add(1L);
}
public long CalculateFibsRec(int number)
{
if (number <= 1)
{
return number;
}
long p1 = CalculateFibsRec(number-1);
long p2 = CalculateFibsRec(number-2);
return p1+p2;
}
public long CalculateFibsIter(int number)
{
if (Fibs.size() >= number) /// It Is Pre-Calced
{
return Fibs.get(number);
}
/**
* Not Pre-Calced so we start filling from where we stopped at last !!
* No Need to Re-Calc from Beginning !!!
* */
int start = Fibs.size();
for (int i=start ; i<= number+1 ; i++)
{
Fibs.add(Fibs.get(i-2) + Fibs.get(i-1));
}
return Fibs.get(number);
}
}
import junit.framework.TestCase;
import java.util.Scanner;
public class FibonacciSeriesTest extends TestCase
{
private FibonacciSeries fibonacciSeries;
int Number;
@Override
protected void setUp()
{
///System.out.println("Enter Number to Get its Fibonacci");
/// Scanner scanner = new Scanner(System.in);
///Number = scanner.nextInt();
fibonacciSeries = new FibonacciSeries();
Number = 40;
}
public void testFibonacciSeriesRec()
{
System.out.println("Using Recursion : ");
long start = System.currentTimeMillis();
long ans = fibonacciSeries.CalculateFibsRec(Number);
long end = System.currentTimeMillis();
System.out.println("Ans = " + (ans));
System.out.println("Time Taken : " + (end-start) + " ms");
}
public void testFibonacciSeriesIter()
{
System.out.println("Using Iterative : ");
long start = System.currentTimeMillis();
long ans = fibonacciSeries.CalculateFibsIter(Number);
long end = System.currentTimeMillis();
System.out.println("Ans = " + (ans));
System.out.println("Time Taken : " + (end-start) + " ms");
}
}
...@@ -16,7 +16,5 @@ public class FindPrimesTest extends TestCase ...@@ -16,7 +16,5 @@ public class FindPrimesTest extends TestCase
{ {
System.out.println(Primes1.get(i)); System.out.println(Primes1.get(i));
}*/ }*/
} }
} }
...@@ -6,10 +6,10 @@ import java.util.Scanner; ...@@ -6,10 +6,10 @@ import java.util.Scanner;
public class SearchForPrimesThreadTest extends TestCase public class SearchForPrimesThreadTest extends TestCase
{ {
private int Start=1; private int Start=6000000;
private int End=10000000; private int End=10000000;
private int ThreadsNum=4; private int ThreadsNum=4;
private int step=2500000; private int step=(End - Start +1)/4;
private List<Integer> primes; private List<Integer> primes;
...@@ -49,6 +49,8 @@ public class SearchForPrimesThreadTest extends TestCase ...@@ -49,6 +49,8 @@ public class SearchForPrimesThreadTest extends TestCase
System.out.println("Time Taken = " + (endTimer-startTimer)); System.out.println("Time Taken = " + (endTimer-startTimer));
System.out.println("size = " + (primes.size()));
/**for (int i=0 ; i<primes.size() ; i++) /**for (int i=0 ; i<primes.size() ; i++)
{ {
System.out.println(primes.get(i)); System.out.println(primes.get(i));
......
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