Created
October 4, 2012 16:14
-
-
Save kazua/3834678 to your computer and use it in GitHub Desktop.
JOJO-Eratosthenes-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
//素数の時だけjojo(エラトステネスの篩) | |
//K.A | |
import scala.math._ | |
object prime { | |
def prime(acl : List[Int], sl : List[Int], ms : Int) : List[Int] = sl match { | |
case Nil => acl.reverse ::: sl | |
case sl => { | |
if (sl.head != 2 && (sl.isEmpty || pow(ms, 2) > sl.last)) acl.reverse ::: sl | |
else { | |
prime(sl.head :: acl, sl.tail.filter(_ % sl.head != 0), sl.head) | |
} | |
} | |
} | |
def jojo(pl : List[Int], sl : List[Int]) { | |
if (pl != Nil && pl.head == sl.head) { | |
println("jojo!") | |
jojo(pl.tail, sl.tail) | |
} else if (sl != Nil) { | |
println(sl.head) | |
jojo(pl, sl.tail) | |
} | |
} | |
def main(args : Array[String]) { | |
val i = 100 //ここまでの数字内で処理 | |
jojo(prime(Nil, Range(2, i + 1).toList, 2), Range(1, i + 1).toList) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment