Create a gist now

Instantly share code, notes, and snippets.

@kei0425 /prime.scala
Last active Jun 20, 2016

Embed
What would you like to do?
Scalaで素数
object Main extends App{
import java.{util => ju}
import scala.annotation._
import scala.collection._
import scala.collection.{mutable => mu}
import scala.collection.JavaConverters._
import scala.math._
val sc = new ju.Scanner(System.in)
/* code here */
def primemain(primelist:List[Int], remain:List[Int], limit:Double):List[Int] = remain match {
case Seq(a, _*) if a > limit => primelist:::remain
case Seq(a, _*) => primemain(primelist:::List(a), remain.tail.filter(x => x % a != 0), limit)
case _ => primelist
}
def prime(n: Int):List[Int] = primemain(List(), (2 to n).toList, sqrt(n))
val n = sc.nextInt()
println(prime(n))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment