Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Created December 18, 2014 15:57
Show Gist options
  • Save soltrinox/337b5c120f07e007d91a to your computer and use it in GitHub Desktop.
Save soltrinox/337b5c120f07e007d91a to your computer and use it in GitHub Desktop.
isPrime test
public static Boolean isPrime(int num){ //method signature. returns Boolean, true if number isPrime, false if not
if(num==2){ //for case num=2, function returns true. detailed explanation underneath
return(true);
}
for(int i=2;i<=(int)Math.sqrt(num)+1;i++){ //loops through 2 to sqrt(num). All you need to check- efficient
if(num%i==0){ //if a divisor is found, its not prime. returns false
return(false);
}
}
return(true); //if all cases don't divide num, it is prime.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment