Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Created April 8, 2015 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modalsoul/2692e79ed93b966e00c8 to your computer and use it in GitHub Desktop.
Save modalsoul/2692e79ed93b966e00c8 to your computer and use it in GitHub Desktop.
Scalaで素数判定その4
import scala.math.sqrt
object PrimeNum {
def main(args:Array[String]):Unit = {
val num:Long = scala.io.StdIn.readLong
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).forall(num%_ != 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment