added lesson 6
This commit is contained in:
29
src/Lesson6/Primes.sc
Normal file
29
src/Lesson6/Primes.sc
Normal file
@@ -0,0 +1,29 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user