Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Last active December 2, 2016 09:51
Show Gist options
  • Select an option

  • Save modalsoul/6960204 to your computer and use it in GitHub Desktop.

Select an option

Save modalsoul/6960204 to your computer and use it in GitHub Desktop.
Scalaで2数値間の素数の個数検出
import scala.math.sqrt
import scala.collection.mutable.ArrayBuffer
object PrimeNumCounter {
def main(args:Array[String]) {
println(countPrimeNum(args(0).toInt, args(1).toInt))
}
def countPrimeNum(start:Int, end:Int) = {
var primeList:ArrayBuffer[Int] = ArrayBuffer[Int](2)
var numList:List[Int] = (3 to end by 2).toList
while(numList.head < sqrt(end)) {
primeList += numList.head
numList = numList.filterNot( e => e%primeList.last == 0 )
}
primeList.count(n=> n >= start) + numList.count(n=> n >= start)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment