Finding a prime number that is one greater than a squared number from a given number [closed]

StackOverflow https://stackoverflow.com/questions/21438803

  •  04-10-2022
  •  | 
  •  

Question

So I have this small method I am working on for class and this question is a tough one and a bit confusing. Basically what I have to to is write a method that will take a number that you input and it will find the next prime number that is 1 greater than a squared number. For example if I were to type in 10 the result would be 17, because 17 is a prime number and its 1 greater than a square, the square being 16. So I have to write a method with only one return statement in it to do this, and I have absolutely no idea where to start. What I have so far ispublic static int nextSquarePlus1Prime(int num){ } Literally. Some helped would be greatly appreciated. I already have this method where I check if the number is prime or not:

public static boolean isPrime(int num){

    int i;
    boolean numberIsPrime=true;
    if(num==1){
        numberIsPrime=false;
    }

    for(i=2;i*i<=num;i++){
        if(num%i==0){
            numberIsPrime=false;
        }
    }

    return numberIsPrime;
}

Is there a way to add to this method with another piece of code to work alongside with this one to check if the number is a square?

So this is what i came up with for my code and i put in 10 as my number. I'm getting 50 when I should be getting 17.

public static int nextSquarePlus1Prime(int num){
    int i;
    int save=0;

    for(i=1;i<=num;i++){
        if(isPrime(i)&& i*i+1>=num){
            save=i*i+1;    
        }
    }

    return save;
}
Was it helpful?

Solution

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;

    }

OTHER TIPS

You should provide some additional details for more specific pointers.

In Java, there is a method "is probable prime" that may be of use to you.

Its easier to square a number than it is to find the square root, so it may make sense to find the square root of the starting number, then round up. Take that number and square it, add 1 and check for prime. If not, add 1 to the variable and try again.

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top