Commit 5327a8d9 authored by abdullh.alsoleman's avatar abdullh.alsoleman

add main

parent f100d128
import java.util.ArrayList;
import java.util.List;
//Abdullah Alsoleman
public class Main {
public static void main(String[] args) throws InterruptedException {
int startRange = 1;
int endRange = 100_000_000;
int numThreads = 4;
int chunkSize = (endRange - startRange + 1) / numThreads;
List<Thread> threads = new ArrayList<>();
List<PrimeNumberService> allChunks_PrimeNumbers = new ArrayList<PrimeNumberService>();
for (int i = 0; i < numThreads; i++) {
int start = startRange + i * chunkSize;
int end = start + chunkSize - 1;
if (i == numThreads - 1) {
end = endRange;
}
PrimeNumberService chunk_primeNumber = new PrimeNumberService(start, end);
Thread thread = new Thread(chunk_primeNumber);
threads.add(thread);
allChunks_PrimeNumbers.add(chunk_primeNumber);
thread.start();
}
long startTime = System.currentTimeMillis();
for (Thread thread : threads) {
thread.join();
}
long endTime = System.currentTimeMillis();
List<Integer> allPrimeNumbers = new ArrayList<>();
for (PrimeNumberService primeNumbers : allChunks_PrimeNumbers) {
allPrimeNumbers.addAll(primeNumbers.getPrimeNumbers());
}
System.out.println("Total prime numbers found: " + allPrimeNumbers.size());
System.out.println("Execution time: " + (endTime - startTime) + " milliseconds");
}
}
import java.util.ArrayList;
import java.util.List;
public class PrimeNumberService implements Runnable {
private int start;
private int end;
private List <Integer> primeNumbers;
public PrimeNumberService() {
}
public PrimeNumberService(int start, int end) {
this.start = start;
this.end = end;
primeNumbers = new ArrayList<Integer>();
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
public List<Integer> getPrimeNumbers() {
return primeNumbers;
}
public void setStart(int start) {
this.start = start;
}
public void setEnd(int end) {
this.end = end;
}
public boolean isPrime (int num){
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public List<Integer> FindPrimeNumbersInRange(){
List <Integer> range_primeNumbers= new ArrayList<>();
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
range_primeNumbers.add(num);
}
}
return range_primeNumbers;
}
@Override
public void run() {
primeNumbers.addAll(FindPrimeNumbersInRange());
}
}
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
import junit.framework.TestCase;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class PrimeNumberServiceTest extends TestCase
{
@Test
public void testIsPrime()
{
PrimeNumberService test = new PrimeNumberService();
assertTrue(test.isPrime(7));
}
@Test
public void testFindPrimeNumbersInRange(){
PrimeNumberService test = new PrimeNumberService(1,20);
List<Integer> expected = Arrays.asList(2,3,5,7,11,13,17,19);
assertEquals(expected,test.FindPrimeNumbersInRange());
}
}
\ 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