I have to write a program where you have a lower bound and an upper bound, and the program has to give all the prime numbers inside that interval. I see a lot of these kinds of question, so I understand it a little, but here is where I am stuck.
I have to use these:
public static void main(String[] args) { public static List getPrimeNumbers(int lowerBound, int upperBound){
I just don't know how to use this to get the right answer.
If my program is called with getPrimeNumbers(2,17)
, it should return the list:
[2, 3, 5, 7, 11, 13, 17]
EDIT:
public class PrimeNumbers {
public static void main(String[] args) {
getPrimeNumbers(int lowerBound, int upperBound);
}
public static List<Integer> getPrimeNumbers(int lowerBound, int upperBound) {
int lowerBound, upperBound,count = 0, i, j;
for (int i = lowerBound; i <= upperBound; i )
{ for (j = 2; j< i; j )
{ if (i % j == 0 )
{ count = 0;
break;
}
else
{
count = 1;
}
if(count == 1)
return // new ArrayList<>();
this is what I have right now
EDIT: I understand!! thankyou for all your inputs!!
CodePudding user response:
Do a for loop in inside the function
public static List<Integer> getPrimeNumbers(int lowerBound, int upperBound){
ArrayList<Integer> primeNumber = new ArrayList<>()
// loop over all numbers in the range and add the number to primeNumber list if it is a prime.
for (int i = lowerBound, i <= upperBound, i ){
if isPrime(i){
primeNumber.add(i);
}
}
return primeNumber;
}
// function to check if a number is prime
public static boolean isPrime(int num){
boolean flag = false;
for (int i = 2; i <= num / 2; i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
}
return flag;
}
CodePudding user response:
For, smaller numbers, you can get away with Sieve of Eratosthenes. For larger ones, this method won't work...
private static boolean sieve[];
private static void eratosthenesSieve(int maxN)
{
sieve = new boolean[maxN 1];
Arrays.fill(sieve, true);//sets all elements of the given array to true
sieve[0] = false;// 0 isn't prime
sieve[1] = false;// 1 isn't prime
for (int i = 2; i <= maxN; i ) {//loop starts at 2 because we already know that 0 and 1 are false...
if (sieve[i]) {//checks if the current number is prime
for (int j = i*2; i <= maxN; j =i) {
sieve[j] = false;//sets the multiples of current prime number to false
}
}
}
}
public static void main(String... args)
{
int min = 0, max = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the range of Prime numbers seprated with a space");
min = sc.nextInt();
max = sc.nextInt();
eratosthenesSieve(max);
do{//loop prints in descending order, if you want ascending change the condition to min <max, check sieve[min] instead of sieve[max] and print min instead of max
if(sieve[max]){
System.out.println(max);
}
}while(max-->min);//loop is min and max inclusive if you want only max exclusive, change this to while loop with same condition, if you want both exclusive change this to while loop with --max>min condition
}
CodePudding user response:
Use isProbablePrime, it is faster than any implementation that you will write.