Last active
December 10, 2015 09:19
-
-
Save kozake/4413586 to your computer and use it in GitHub Desktop.
Project Euler Problem 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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