Commit f691f516 authored by sahar.moalla's avatar sahar.moalla

add main.java

parents
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws InterruptedException {
int rangeStart = 1;
int rangeEnd = 100_000_000;
int threadsNumber = 4;
int partialRange = (rangeEnd-rangeStart)/threadsNumber;
List<PrimeNumberGenerator> primeNumberGeneratorList = new ArrayList<PrimeNumberGenerator>();
List<Thread> threadList = new ArrayList<Thread>();
List<Integer> primeNumbers = new ArrayList<Integer>();
for(int i = 0 ; i<threadsNumber ; i++){
int partialRangeStart = rangeStart + partialRange* i;
int partialRangeEnd = partialRangeStart + partialRange - 1;
PrimeNumberGenerator primeNumberGenerator = new PrimeNumberGenerator(partialRangeStart,partialRangeEnd);
Thread thread = new Thread(primeNumberGenerator);
threadList.add(thread);
primeNumberGeneratorList.add(primeNumberGenerator);
thread.start();
}
long startTime = System.currentTimeMillis();
for (Thread thread : threadList) {
thread.join();
}
long endTime = System.currentTimeMillis();
for (PrimeNumberGenerator primeNumberGenerator : primeNumberGeneratorList) {
primeNumbers.addAll(primeNumberGenerator.getGeneratedPrimeNumbers());
}
System.out.println("Number of prime numbers found: " + primeNumbers.size());
System.out.println("Execution time: " + (endTime - startTime) + " milliseconds");
}
}
import java.util.ArrayList;
import java.util.List;
public class PrimeNumberGenerator implements Runnable{
public PrimeNumberGenerator(int rangeStart, int rangeEnd) {
this.setRangeStart(rangeStart);
this.setRangeEnd(rangeEnd);
generatedPrimeNumbers = new ArrayList<Integer>();
}
public PrimeNumberGenerator() {
}
public int getRangeStart() {
return rangeStart;
}
public int getRangeEnd() {
return rangeEnd;
}
public void setRangeStart(int rangeStart) {
this.rangeStart = rangeStart;
}
public void setRangeEnd(int rangeEnd) {
this.rangeEnd = rangeEnd;
}
public List<Integer> getGeneratedPrimeNumbers() {
return generatedPrimeNumbers;
}
public boolean checkPrime(int number){
if(number <= 1) return false;
else if (number == 2) return true;
else{
for(int i = 2 ; i <= Math.sqrt(number) ; i++){
if(number % i == 0){
return false;
}
}
return true;
}
}
private int rangeStart;
private int rangeEnd;
private List<Integer> generatedPrimeNumbers;
@Override
public void run() {
int i = this.getRangeStart();
for( ; i < this.getRangeEnd() ; i++){
if(this.checkPrime(i)) generatedPrimeNumbers.add(i);
}
}
}
import junit.framework.TestCase;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class PrimeNumberGeneratorTest extends TestCase {
@Test
public void testcheckPrime() {
PrimeNumberGenerator primeNumberGenerator = new PrimeNumberGenerator();
assertTrue(primeNumberGenerator.checkPrime(23));
}
}
\ 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