Created
December 18, 2014 15:57
-
-
Save soltrinox/337b5c120f07e007d91a to your computer and use it in GitHub Desktop.
isPrime test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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