Commit 82c80313 authored by hasan  khaddour's avatar hasan khaddour

Add Primes Number Finding Task

parent f80984c6
package task;
import java.util.ArrayList;
import java.util.List;
public class TaskPrimeNumbers extends TaskImpl {
@Override
public void execute() {
try {
// The input will be the upper limit for prime generation
int upperLimit = Integer.parseInt(input);
List<Integer> primes = generatePrimes(upperLimit);
// Write the result as string
result = "Found " + primes.size() + " prime numbers up to " + upperLimit;
} catch (NumberFormatException e) {
result = "Invalid input, Please provide a valid number as the upper limit of the interval.";
}
}
// this function to generate prime numbers up to a certain limit
private List<Integer> generatePrimes(int limit) {
List<Integer> primes = new ArrayList<>();
for (int i = 2; i <= limit; i++) {
if (isPrime(i)) {
primes.add(i);
}
}
return primes;
}
// this function to check if a number is prime
private boolean isPrime(int number) {
if (number <= 1) return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % 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