Commit 31612912 authored by layla.halaoua's avatar layla.halaoua

first

parents
# Default ignored files
/shelf/
/workspace.xml
Test.java
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="HW1_ParallelComputing" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/parallel-computing-hw1" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>HW1_ParallelComputing</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package org.example;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
}
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.List;
import junit.
public class Test {
public static void main(String[] args) {
List<Integer> primes = new ArrayList<>();
List<Thread> threads = new ArrayList<>();
int numberOfThreads = 12;
int maxNumber = 100000000;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numberOfThreads; i++) {
int start = i * (maxNumber / numberOfThreads);
int end = (i + 1) * (maxNumber / numberOfThreads) - 1;
Thread thread = new Thread(() -> extractPrimes(primes, start, end));
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Main function handling the results from the four functions
// (e.g., printing or further processing)
handleResults(primes);
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.println("Execution time: " + executionTime + " ms");
}
private static void extractPrimes(List<Integer> primes, int start, int end) {
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
synchronized (primes) {
primes.add(i);
}
}
}
}
private static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
private static void handleResults(List<Integer> primes) {
// Handle the results here (e.g., print or further processing)
System.out.println("Total prime numbers found: " + primes.size());
}
}
//Total prime numbers found: 5761455
// 5 -> Execution time: 30488 ms
// 6 -> Execution time: 28246 ms
// 7 -> Execution time: 25785 ms
// 8 -> Execution time: 24829 ms
// 9 -> Execution time: 24711 ms
// 10-> Execution time: 24343 ms
// 11-> Execution time: 24898 ms
// 12-> Execution time: 25859 ms
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