Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Created October 10, 2013 13:46
Show Gist options
  • Save modalsoul/6918594 to your computer and use it in GitHub Desktop.
Save modalsoul/6918594 to your computer and use it in GitHub Desktop.
Scalaで素数判定その2
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 by 2).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