Skip to content

Instantly share code, notes, and snippets.

@barsumek
Created April 7, 2017 17:48
Show Gist options
  • Save barsumek/dcca02d17b2d234933259f0dfffe5794 to your computer and use it in GitHub Desktop.
Save barsumek/dcca02d17b2d234933259f0dfffe5794 to your computer and use it in GitHub Desktop.
Task 1
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