Commit facfc59b authored by Lenovo's avatar Lenovo

fourth step with main tester

parent d57be0bb
......@@ -4,9 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="2698c064-0a25-4fa9-b373-a085833ed750" name="Changes" comment="first step">
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/summers/Summer.java" afterDir="false" />
</list>
<list default="true" id="2698c064-0a25-4fa9-b373-a085833ed750" name="Changes" comment="third step" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......@@ -54,7 +52,21 @@
<option name="project" value="LOCAL" />
<updated>1700070627028</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="second step">
<created>1700070804720</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1700070804720</updated>
</task>
<task id="LOCAL-00003" summary="third step">
<created>1700071001900</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1700071001900</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
......@@ -70,6 +82,8 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="first step" />
<option name="LAST_COMMIT_MESSAGE" value="first step" />
<MESSAGE value="second step" />
<MESSAGE value="third step" />
<option name="LAST_COMMIT_MESSAGE" value="third step" />
</component>
</project>
\ No newline at end of file
......@@ -7,7 +7,19 @@
<groupId>org.example</groupId>
<artifactId>ExecutorServiceHW</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
......
package org.example;
import org.example.data.TestData;
import org.example.parallelsummers.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
/*
Use Main function to make unit testing
*/
int start = 0;
int end = 20;
int[] data = TestData.createData(start, end);
long result = SequentialSummer.sum(SequentialSummer.allPrimes(data));
System.out.println("result is " + result);
int threadCount = 25;
System.out.println("Sequential summer:" + result);
System.out.println("Parallel summer 1: " + ParallelSummer0.sum(data, threadCount));
System.out.println("Parallel summer 2: " + ParallelSummer1.sum(data, threadCount));
System.out.println("Parallel summer 3: " + ParallelSummer2.sum(data, threadCount));
System.out.println("Parallel summer 4: " + ParallelSummer3.sum(data, threadCount));
System.out.println("Parallel summer 5: " + ParallelSummer4.sum(data, threadCount));
}
}
\ No newline at end of file
package org.example;
import java.util.ArrayList;
import java.util.List;
/*
The SequentialSummer class calculate the number of primes sequentially.
*/
public class SequentialSummer {
public static long sum(List<Integer> data) {
long sum = data.size();
return sum;
}
/*
The allPrimes function find all primes in the data array sequentially using isPrime function.
*/
public static List<Integer> allPrimes(int[] data)
{
List<Integer> primes = new ArrayList<>();
for (int i = data[0]; i <= data[data.length -1]; i++) {
if (isPrime(i)) {
primes.add(i);
}
}
return primes;
}
private static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i*i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
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