Skip to content

Instantly share code, notes, and snippets.

@kozake
Last active December 10, 2015 09:19
Show Gist options
  • Save kozake/4413586 to your computer and use it in GitHub Desktop.
Save kozake/4413586 to your computer and use it in GitHub Desktop.
Project Euler Problem 10
/**
* Project Euler Problem 10
*/
object P10 {
def primes(limit:Int): List[Int] = {
require(limit > 1)
def go(primes:List[Int], search:List[Int]):List[Int] = {
val prime = search.head
if (prime * prime < limit)
go(prime :: primes, search.tail.filterNot(_ % prime == 0))
else
primes.reverse ++ search
}
go(Nil, (2 to limit).toList)
}
def answer():Long = primes(2000000).foldLeft(0l)(_ + _)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment