Skip to content

Instantly share code, notes, and snippets.

@pfcoperez
Created November 9, 2017 15:59
Show Gist options
  • Save pfcoperez/95fb138c8cf9ac77a01780523dc6c02a to your computer and use it in GitHub Desktop.
Save pfcoperez/95fb138c8cf9ac77a01780523dc6c02a to your computer and use it in GitHub Desktop.
val upperBound = 100
val candidates = sc.range(1, upperBound)
val factors = sc.range(1, math.sqrt(upperBound).toLong+1)
val primes = {
candidates.cartesian(factors) filter {
case (candidate, factor) => candidate % factor == 0L
}
}.aggregateByKey(0L)((counter, _) => counter+1L, _ + _).filter(_._2 <= 2L).keys
// How many primes are under the number 100?
primes.count
// Let's get 15 first...
primes.sortBy(identity).take(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment