Given two integers N and M (N ΓΆβ?°Β¤ M), output all the prime numbers between N and M inclusive, one per line.
N and M will be positive integers less than or equal to 1,000,000,000. The difference between N and M will be less than or equal to 5,000,000. Sample Input
5 20 Sample Output
5 7 11 13 17 19
import java.util.*;
class FindPrime{
static boolean isPrime(long number) {
boolean isPrime = false;
int i = (int) Math.ceil(Math.sqrt(number));
while (i > 1) {
if ((number != i) && (number % i == 0)) {
isPrime = false;
break;
} else if (!isPrime)
isPrime = true;
--i;
}
return isPrime;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter N: ");
long num1 = input.nextLong();
System.out.print("Enter M: ");
long num2 = input.nextLong();
for (long i = num1; i <=num2; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
}