Skip to content

Instantly share code, notes, and snippets.

@benkolera
Created March 30, 2011 01:58
Show Gist options
  • Save benkolera/893725 to your computer and use it in GitHub Desktop.
Save benkolera/893725 to your computer and use it in GitHub Desktop.
Prime Sieve. No arguments about whether 1 is prime... :p
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