Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Created October 9, 2013 14:38
Show Gist options
  • Save modalsoul/6902340 to your computer and use it in GitHub Desktop.
Save modalsoul/6902340 to your computer and use it in GitHub Desktop.
Scalaで素数判定
import scala.math.sqrt
object PrimeNum {
def main(args:Array[String]) {
val num:Long = args(0).toLong
if(isPrime(num)) println(num + " is Prime Number.")
else println(num + " is NOT Prime Number.")
}
def isPrime(num:Long):Boolean = {
if (num == 1 || num%2==0) {
false
} else {
(3L to sqrt(num).toLong).filter(
n => n%2 != 0
).foreach { e =>
if(num%e == 0) return false
}
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment