Skip to content

Instantly share code, notes, and snippets.

@kozake
Last active December 10, 2015 02:28
Show Gist options
  • Save kozake/4367461 to your computer and use it in GitHub Desktop.
Save kozake/4367461 to your computer and use it in GitHub Desktop.
Project Euler Problem 7
/**
* Project Euler Problem 7
*/
object P7 {
def from(n:Long):Stream[Long] = n #:: from(n + 1)
def primes(): Stream[Long] = {
import scala.collection.mutable.ListBuffer
def f(ps: ListBuffer[Long], l: Stream[Long]): Stream[Long] = {
val head = l.find(x => !ps.exists(x % _ ==0)).get
head #:: f(ps += head, from(head + 1))
}
f(new ListBuffer[Long], from(2))
}
def answer():Long = primes.take(10001).last
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment