Commit 543c3868 authored by julanar.ali's avatar julanar.ali

Initial commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?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="PrimerFinder" />
</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_11" default="true" project-jdk-name="11" 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 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>PrimerFinder</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package PrimerFinder;
import java.util.ArrayList;
import java.util.List;
public class ParallelPrimerFinder {
private int numberOfThreads;
public ParallelPrimerFinder(int numberOfThreads) {
this.numberOfThreads = numberOfThreads;
}
public List<Integer> primesFinder(int rangeStart,int rangeEnd) {
int threadRange = (rangeEnd - rangeStart) / numberOfThreads;
List<Integer> primes = new ArrayList<>();
List<Integer> finalPrimes = new ArrayList<>();
List<PrimerFinder> threads = new ArrayList<>();
for (int i = 0; i < numberOfThreads ; i++) {
int start = rangeStart + i * threadRange;
int end = (i < numberOfThreads - 1) ? start + threadRange : rangeEnd;
PrimerFinder thread = new PrimerFinder(start, end, primes);
threads.add(thread);
thread.start();
}
try {
for (PrimerFinder thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return primes;
}
}
package PrimerFinder;
import java.util.List;
public class PrimerFinder extends Thread{
private int start;
private int end;
public List<Integer> getPrimeNumList() {
return primeNumList;
}
private List<Integer> primeNumList;
public PrimerFinder(int start,int end,List<Integer> primes){
this.start=start;
this.end=end;
this.primeNumList=primes;
}
public boolean isPrime(int num){
if(num<2){
return false;
}
else{
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
public void run(){
for (int i = start; i < end; i++) {
if (isPrime(i)) {
synchronized (primeNumList) {
primeNumList.add(i);
}
}
}
}
}
package Runnable;
import PrimerFinder.PrimerFinder;
import PrimerFinder.ParallelPrimerFinder;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int threadsNum=100;
int rangeStart = 1;
int rangeEnd = 100000000;
long starter= System.currentTimeMillis();
ParallelPrimerFinder parallelPrimerFinder=new ParallelPrimerFinder(threadsNum);
List<Integer> primes = parallelPrimerFinder.primesFinder(rangeStart,rangeEnd);
long end=System.currentTimeMillis();
System.out.println("Prime numbers between 1 and 100,000,000: " + primes);
long time=end-starter;
System.out.println("The Time of finding Primes numbers between 1 and 100,000,000: "+time +" Millis");
}
}
\ No newline at end of file
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