Commit 54c312d7 authored by mohammad.salama's avatar mohammad.salama

Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci...

Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)+ Fibonacci with Threads (simple&Complex)
parent b125fd89
...@@ -4,13 +4,11 @@ ...@@ -4,13 +4,11 @@
<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="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)"> <list default="true" id="156428c6-b9a7-4d3a-9504-b3537029ac6a" name="Changes" comment="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)+ Fibonacci with Threads (simple)">
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/FibonacciSeriesThreadInRecursive.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/FibonacciSeriesThreadInRecursiveTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/FibonacciSeriesService.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/FibonacciSeriesThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/FibonacciSeriesThreadTest.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/main/java/FibonacciSeriesThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/FibonacciSeriesThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/FibonacciSeriesTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/FibonacciSeriesTest.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/test/java/FibonacciSeriesTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/FibonacciSeriesTest.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
...@@ -47,7 +45,7 @@ ...@@ -47,7 +45,7 @@
"last_opened_file_path": "D:/HIAST/FIY/FS/Parallel Programming/Lab/1/PP-lec01-Threads" "last_opened_file_path": "D:/HIAST/FIY/FS/Parallel Programming/Lab/1/PP-lec01-Threads"
} }
}]]></component> }]]></component>
<component name="RunManager" selected="JUnit.FibonacciSeriesThreadTest.testFibonacciThread"> <component name="RunManager" selected="JUnit.FibonacciSeriesThreadInRecursiveTest.testFibonacciThread">
<configuration name="FibonacciSeriesTest.testFibonacciSeriesIter" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="FibonacciSeriesTest.testFibonacciSeriesIter" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" /> <module name="Lab101" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
...@@ -68,21 +66,22 @@ ...@@ -68,21 +66,22 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="FibonacciSeriesThreadTest.testFibonacciThread" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="FibonacciSeriesThreadInRecursiveTest.testFibonacciThread" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" /> <module name="Lab101" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="FibonacciSeriesThreadTest" /> <option name="MAIN_CLASS_NAME" value="FibonacciSeriesThreadInRecursiveTest" />
<option name="METHOD_NAME" value="testFibonacciThread" /> <option name="METHOD_NAME" value="testFibonacciThread" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="SearchForPrimesThreadTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="FibonacciSeriesThreadTest.testFibonacciThread" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" /> <module name="Lab101" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SearchForPrimesThreadTest" /> <option name="MAIN_CLASS_NAME" value="FibonacciSeriesThreadTest" />
<option name="TEST_OBJECT" value="class" /> <option name="METHOD_NAME" value="testFibonacciThread" />
<option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
...@@ -99,11 +98,11 @@ ...@@ -99,11 +98,11 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="JUnit.FibonacciSeriesThreadTest.testFibonacciThread" /> <item itemvalue="JUnit.FibonacciSeriesThreadInRecursiveTest.testFibonacciThread" />
<item itemvalue="JUnit.FibonacciSeriesTest.testFibonacciSeriesIter" /> <item itemvalue="JUnit.FibonacciSeriesTest.testFibonacciSeriesIter" />
<item itemvalue="JUnit.FibonacciSeriesThreadTest.testFibonacciThread" />
<item itemvalue="JUnit.FibonacciSeriesTest.testFibonacciSeriesRec" /> <item itemvalue="JUnit.FibonacciSeriesTest.testFibonacciSeriesRec" />
<item itemvalue="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest" /> <item itemvalue="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest" />
<item itemvalue="JUnit.SearchForPrimesThreadTest" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
...@@ -144,7 +143,14 @@ ...@@ -144,7 +143,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1698480438448</updated> <updated>1698480438448</updated>
</task> </task>
<option name="localTasksCounter" value="5" /> <task id="LOCAL-00005" summary="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)+ Fibonacci with Threads (simple)">
<created>1698483044948</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1698483044948</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
...@@ -163,6 +169,7 @@ ...@@ -163,6 +169,7 @@
<MESSAGE value="Creating Simple Finding Primes in Interval" /> <MESSAGE value="Creating Simple Finding Primes in Interval" />
<MESSAGE value="Finding Primes from 1 to 10^7 using multi-threading" /> <MESSAGE value="Finding Primes from 1 to 10^7 using multi-threading" />
<MESSAGE value="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)" /> <MESSAGE value="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)" />
<option name="LAST_COMMIT_MESSAGE" value="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)" /> <MESSAGE value="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)+ Fibonacci with Threads (simple)" />
<option name="LAST_COMMIT_MESSAGE" value="Finding Primes from 1 to 10^7 using multi-threading+Finding Fibonacci (Rec+Iter)+ Fibonacci with Threads (simple)" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -16,6 +16,7 @@ public class FibonacciSeriesThread extends Thread ...@@ -16,6 +16,7 @@ public class FibonacciSeriesThread extends Thread
{ {
return number; return number;
} }
long p1 = CalculateFibsRec(number-1); long p1 = CalculateFibsRec(number-1);
long p2 = CalculateFibsRec(number-2); long p2 = CalculateFibsRec(number-2);
return p1+p2; return p1+p2;
......
import java.util.ArrayList;
public class FibonacciSeriesThreadInRecursive implements Runnable
{
public long CalculateFibsRec(long number)
{
if (number <= 1)
{
return number;
}
final long[] p1 = {0};
Runnable R1 = new Runnable() {
@Override
public void run() {
p1[0] = CalculateFibsRec(number-1);
}
};
final long[] p2 = {0};
Runnable R2 = new Runnable() {
@Override
public void run() {
p2[0] = CalculateFibsRec(number-2);
}
};
Thread thread1 = new Thread(R1);
Thread thread2 = new Thread(R2);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
}
catch (InterruptedException e)
{
}
return p1[0]+p2[0];
}
@Override
public void run()
{
}
}
...@@ -14,7 +14,7 @@ public class FibonacciSeriesTest extends TestCase ...@@ -14,7 +14,7 @@ public class FibonacciSeriesTest extends TestCase
/// Scanner scanner = new Scanner(System.in); /// Scanner scanner = new Scanner(System.in);
///Number = scanner.nextInt(); ///Number = scanner.nextInt();
fibonacciSeries = new FibonacciSeries(); fibonacciSeries = new FibonacciSeries();
Number = 45; Number = 20;
} }
public void testFibonacciSeriesRec() public void testFibonacciSeriesRec()
......
import junit.framework.TestCase;
public class FibonacciSeriesThreadInRecursiveTest extends TestCase
{
private FibonacciSeriesThreadInRecursive fibonacciSeriesThreadInRecursive;
public void testFibonacciThread()
{
long num = 20;
fibonacciSeriesThreadInRecursive = new FibonacciSeriesThreadInRecursive();
long start = System.currentTimeMillis();
long ans = fibonacciSeriesThreadInRecursive.CalculateFibsRec(num);
long end = System.currentTimeMillis();
System.out.println("Ans = " + (ans));
System.out.println("Time Taken = " + (end-start));
}
}
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