29 lines
687 B
Scala
29 lines
687 B
Scala
def time(f: => Unit => Any): Long = {
|
|
val start = System.currentTimeMillis()
|
|
f()
|
|
val end = System.currentTimeMillis()
|
|
end - start
|
|
}
|
|
|
|
def timeVerbose(s: String)(f: => Unit => Any): Unit = {
|
|
println(s"[$s] Measuring time...")
|
|
val duration = time(f)
|
|
println(s"[$s] It took ${duration}ms")
|
|
}
|
|
|
|
def isPrime(i: Int): Boolean =
|
|
i match {
|
|
case i if i <= 1 => false
|
|
case 2 => true
|
|
case _ => !(2 until i).exists(x => i%x == 0)
|
|
}
|
|
|
|
def primeSum(max: Int)(isPrimeFunc: Int => Boolean): List[(Int, Int)] = {
|
|
for (i <- (1 to max).toList;
|
|
j <- (1 to max).toList;
|
|
if isPrimeFunc(i + j)) yield (i, j)
|
|
}
|
|
|
|
timeVerbose("Normal Prime"){
|
|
primeSum(1000)(isPrime)
|
|
} |