Skip to content

Instantly share code, notes, and snippets.

@fedesilva
Created April 20, 2010 13:07
Show Gist options
  • Save fedesilva/372405 to your computer and use it in GitHub Desktop.
Save fedesilva/372405 to your computer and use it in GitHub Desktop.
scala> f@Hedwig:~ $ scala
Welcome to Scala version 2.8.0.r21323-b20100403020201 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import java.lang.Math._
import java.lang.Math._
scala> def hasDivisor(i:Int) = { (2 to sqrt(i).toInt).exists{ x => i%x == 0 } }
hasDivisor: (i: Int)Boolean
scala> def isPrime(i:Int) = ( i>1 && i%i == 0 && !hasDivisor(i) )
isPrime: (i: Int)Boolean
scala> val primes = List(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)
primes: List[Int] = List(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)
scala> (0 to 100).filter(isPrime).toList == primes
res0: Boolean = true
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment