The sqrt() method may help. Here is an idea of how to use it:
int squareRoot = (int) Math.sqrt((double) input);
Input being the number you want to round. Casting the result to an int will automatically round it. It is optional. I cast the input to a double, but you only need to do so if your input in an int.
Here is an easier way to check if a number is prime:
if (input % 2 == 0){
input += 1;
}
return input;
I reccomend you look at @useSticks answer again. Using the approach he described I created a method that does exactly what I think you want.
Here is a method that finds the square root of a positive number:
public static int sqrt(double number){
double g1;
if(number==0){
return 0;
}
double squareRoot = number/2;
do{
g1=squareRoot;
squareRoot = (g1 + (number/g1))/2;
}while((g1-squareRoot)!=0);
return (int) squareRoot;
}
7
solved Finding a prime number that is one greater than a squared number from a given number [closed]