I have the following method for determining whether or not an integer value is a prime number:
public boolean isPrime(int value){ if(value < 2) return false; else { int xSquareRoot = (int)Math.sqrt((double)value); for (int x = 3; x <= xSquareRoot; x+=2) if(value % x == 0) return false; } return true; }
The for loop seems like it would be a waste of time and resources, and that it would be inefficient.
I'm just wondering if there's maybe a way I could make this more efficient. I mean, it works very quickly now, but it just seems like iterating over every odd number up to the integer square root of a number seems like a resource drain, especially for large primes, where the code would have to do several thousand iterations.