Run Code  | API  | Code Wall  | Users  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

Prime or Not

 run  | edit  | history  | help 0

I suggest to go from i to Math.pow(n,.5) (You will need to import it) && flag because you don't need to check the other side of the square, and instead of s++ use flag = false in replacement of it... such as it runs quicker and you don't need to count the instances of the factors if is simply supposed to return true/false.

int n= sc.nextInt();

boolean flag = true;

for (int i=2;i<=Math.pow(n,.5) && flag;i++) {

if (n%i==0) { 

     flag = false;



if (flag) {

System.out.println(n + " is a Prime Number.");

} else {

System.out.println(n + "is not a Prime number");


  by  redrumze, 3 years ago

Please log in to post a comment.