Commit 8bca5127 authored by roset.alwzrah's avatar roset.alwzrah

calculate fibonacci number

parent cb8822d8
Pipeline #179 canceled with stages
......@@ -4,15 +4,10 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="bd82bf16-4018-4075-b7d4-6d792f3cd58b" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" 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/Service/NumOfOccurrences.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/NumOfOccurrencesTest.java" afterDir="false" />
<list default="true" id="bd82bf16-4018-4075-b7d4-6d792f3cd58b" name="Changes" comment="Find the number of occurrences of a number in an array">
<change afterPath="$PROJECT_DIR$/src/main/java/Service/Fibonacci.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/FibonacciTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -54,6 +49,14 @@
<option name="presentableId" value="Default" />
<updated>1679420459839</updated>
</task>
<task id="LOCAL-00001" summary="Find the number of occurrences of a number in an array">
<created>1679420949412</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1679420949412</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
......@@ -67,4 +70,8 @@
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Find the number of occurrences of a number in an array" />
<option name="LAST_COMMIT_MESSAGE" value="Find the number of occurrences of a number in an array" />
</component>
</project>
\ No newline at end of file
package Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.RecursiveTask;
/*
However, besides being a dumb way to compute Fibonacci functions (there is a simple fast linear algorithm that you'd use in practice),
this is likely to perform poorly because the smallest subtasks are too small to be worthwhile splitting up.
Instead, as is the case for nearly all fork/join applications,
you'd pick some minimum granularity size (for example 10 here) for which you always sequentially solve rather than subdividing.
*/
public class Fibonacci extends RecursiveTask<Integer> {
final int n;
public static HashMap <Integer, Integer> fibValues = new HashMap<Integer, Integer>(); /////
public Fibonacci(int n) { this.n = n; }
public Integer compute() {
if(n > 20) {
if (n <= 1)
return n;
Fibonacci f1 = new Fibonacci(n - 1);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2);
return f2.compute() + f1.join();
}else{
return computeSeq();
}
}
public Integer computeSeq() {
if (n <= 1)
return n;
else if (fibValues.containsKey(n))
return fibValues.get(n);
else {
Fibonacci f1 = new Fibonacci(n - 1);
Fibonacci f2 = new Fibonacci(n - 2);
int value=f2.computeSeq() + f1.computeSeq();
fibValues.put(n,value);
return value;
}
}
}
import Service.Fibonacci;
import junit.framework.TestCase;
public class FibonacciTest extends TestCase {
int n=40;
public void testFiboPP(){
long start = System.currentTimeMillis();
Fibonacci fib = new Fibonacci(n);
int res = fib.compute();
long end = System.currentTimeMillis()-start;
System.out.printf("Fibonacci for %d is %d, and parallel execution took %d ms\n",n,res,end);
// Fibonacci parallel takes 144269 fon n = 50 before enhancement 1
}
public void testFiboSeq(){
long start = System.currentTimeMillis();
Fibonacci fib = new Fibonacci(n);
int res = fib.computeSeq();
long end = System.currentTimeMillis()-start;
System.out.printf("Fibonacci for %d is %d, and sequential execution took %d ms\n",n,res,end);
//Fibonacci Sequential takes 86495 fon n = 50 before enhancement 1
}
}
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