Skip to content

Instantly share code, notes, and snippets.

@noel-yap
Last active November 29, 2016 19:42
Show Gist options
  • Save noel-yap/5fa4a3c4c37b856fd18f79293906b6b6 to your computer and use it in GitHub Desktop.
Save noel-yap/5fa4a3c4c37b856fd18f79293906b6b6 to your computer and use it in GitHub Desktop.
#!/bin/bash
exec /opt/scala-2.10/bin/scala -feature "$0" "$@"
!#
object Primes {
private lazy val notDivisibleBy2: Stream[Long] = 3L #:: notDivisibleBy2.map(_ + 2)
private lazy val notDivisibleBy2Or3: Stream[Long] = notDivisibleBy2
.grouped(3)
.map(_.slice(1, 3))
.flatten
.toStream
private lazy val notDivisibleBy2Or3Or5: Stream[Long] = notDivisibleBy2Or3
.grouped(10)
.map { g =>
g.slice(1, 7) ++ g.slice(8, 10)
}
.flatten
.toStream
lazy val primes: Stream[Long] = 2L #::
notDivisibleBy2.head #::
notDivisibleBy2Or3.head #::
notDivisibleBy2Or3Or5.filter { i =>
i < 49 || primes.takeWhile(_ <= Math.sqrt(i).toLong).forall(i % _ != 0)
}
def <(n: Long): Stream[Long] = primes.takeWhile(_ <= n)
}
println((Primes < 1048576).last)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment