Last active
June 20, 2016 07:54
-
-
Save kei0425/a6e0d3d89053ef9da7b946c2054e0cec to your computer and use it in GitHub Desktop.
Scalaで素数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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