Created
April 7, 2017 17:48
-
-
Save barsumek/dcca02d17b2d234933259f0dfffe5794 to your computer and use it in GitHub Desktop.
Task 1
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 Sequences { | |
def filterPrimeCount(toFilter: Seq[Int], toCheckPrimeCount: Seq[Int]): Seq[Int] = { | |
def isPrime(num: Int): Boolean = { | |
require(num >= 0) | |
num match { | |
case n if n == 2 || n == 3 => true | |
case n if n < 2 || n % 2 == 0 => false | |
case _ => 5.to(math.sqrt(num).toInt, 2).forall(num % _ != 0) | |
} | |
} | |
val numberIsPrimeMap: Map[Int, Boolean] = toCheckPrimeCount | |
.groupBy(identity) | |
.map { case (num, grouped) => (num, isPrime(grouped.length)) } | |
toFilter.filter(n => !numberIsPrimeMap.getOrElse(n, false)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment