The question asked to find all the prime numbers within a particular range. The way I approached this was to loop through each of the numbers within the range and for each number check whether it's a prime. My method for checking prime is to start at 3 and loop till number/2 in hops of 2(essentially excluding all the even numbers). Can somebody take a look at the code an tell me how I might be able to better this snippet and what are some of the aspects that I am missing.
public class PrimeBetween{
public static void main(String[] args){
printAllPrimes(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
}
public static void printAllPrimes(int start,int end){
for(int i = start;i <= end;i++){
if(isPrime(i))
System.out.println("Print prime:"+i);
}
}
private static boolean isPrime(int i){
if(i%2 == 0 && i!=2)
return false;
else{
if(i == 1) return false;
for(int p=3;p<=i/2;p+=2){
if(i%p == 0)
return false;
}
return true;
}
}
}
Thanks