Created
February 24, 2016 15:25
-
-
Save hugolu/6fa97f042a10c1ae1c29 to your computer and use it in GitHub Desktop.
to determine whether a number is a prime
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
def isPrime(i: Int) = | |
if (i <= 1) | |
false | |
else if (i == 2) | |
true | |
else | |
!(2 to i/2).exists(x => i % x == 0) //> isPrime: (i: Int)Boolean | |
isPrime(2) //> res0: Boolean = true | |
isPrime(3) //> res1: Boolean = true | |
isPrime(4) //> res2: Boolean = false | |
isPrime(5) //> res3: Boolean = true | |
isPrime(6) //> res4: Boolean = false | |
isPrime(7) //> res5: Boolean = true | |
isPrime(8) //> res6: Boolean = false | |
isPrime(9) //> res7: Boolean = false | |
isPrime(10) //> res8: Boolean = false | |
isPrime(11) //> res9: Boolean = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment