Created
March 30, 2011 01:58
-
-
Save benkolera/893725 to your computer and use it in GitHub Desktop.
Prime Sieve. No arguments about whether 1 is prime... :p
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
import java.util.Calendar; | |
def sieve(primes:List[Int], possible:List[Int]):List[Int] = | |
possible match { | |
case Nil => primes | |
case firstPrime :: xs => { | |
sieve(firstPrime :: primes , xs.filter( _ % firstPrime != 0) ) | |
} | |
} | |
val start = Calendar.getInstance().getTimeInMillis(); | |
val res = sieve(Nil,(2 to 100000).toList) | |
val end = Calendar.getInstance().getTimeInMillis(); | |
println( "Found %d primes in %f seconds.".format( | |
res.size ,(end - start) / 1000.0 ) | |
); | |
// scala sieve.scala | |
// Found 9592 primes in 2.777000 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment