fork download
  1. import java.io.{BufferedReader, InputStreamReader}
  2. def combinations[T](k: Int, list: List[T]) : List[List[T]] = {
  3. | list match {
  4. | case Nil => Nil
  5. | case head :: xs =>
  6. | if (k <= 0 || k > list.length) {
  7. | Nil
  8. | } else if (k == 1) {
  9. | list.map(List(_))
  10. | } else {
  11. | combinations(k-1, xs).map(head :: _) ::: combinations(k, xs)
  12. | }
  13. | }
  14. | }
  15. combinations: [T](Int,List[T])List[List[T]]
  16.  
  17. scala> case class S99Int(val i: Int) {
  18. |
  19. | def isPrime : Boolean = {
  20. | if (i <= 1) false
  21. | else if (i == 2) true
  22. | else !(2 to (i-1)).exists(x => i % x == 0)
  23. | }
  24. |
  25. | def goldbach : Option[List[Int]] = {
  26. | val primes = (3 to i).filter(x => S99Int(x).isPrime).toList
  27. | combinations(2, primes).find(_.foldLeft(0)((a,b) => a+b) == i)
  28. | }
  29. |
  30. | }
  31. defined class S99Int
  32.  
  33. scala> implicit def i2S99(i: Int) : S99Int = new S99Int(i)
  34. i2S99: (Int)S99Int
  35.  
  36. scala> def printGoldbachListLimited(r: Range, lowerBound: Int) = {
  37. | r.foreach(i => i.goldbach match {
  38. | case Some(List(a,b)) if a > lowerBound => println("%d = %d + %d".format(i,a,b))
  39. | case _ =>
  40. | })
  41. | }
  42. printGoldbachListLimited: (Range,Int)Unit
  43.  
  44. scala> def printGoldbachList(r: Range) = {
  45. | printGoldbachListLimited(r, 0)
  46. | }
  47. printGoldbachList: (Range)Unit
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.scala:2: error: expected class or object definition
def combinations[T](k: Int, list: List[T]) : List[List[T]] = {
^
Main.scala:15: error: expected class or object definition
combinations: [T](Int,List[T])List[List[T]]
^
Main.scala:17: error: expected class or object definition
scala> case class S99Int(val i: Int) {
^
Main.scala:31: error: expected class or object definition
defined class S99Int
^
Main.scala:33: error: expected class or object definition
scala> implicit def i2S99(i: Int) : S99Int = new S99Int(i)
^
Main.scala:34: error: expected class or object definition
i2S99: (Int)S99Int
^
Main.scala:36: error: expected class or object definition
scala> def printGoldbachListLimited(r: Range, lowerBound: Int) = {
^
Main.scala:42: error: expected class or object definition
printGoldbachListLimited: (Range,Int)Unit
^
Main.scala:44: error: expected class or object definition
scala> def printGoldbachList(r: Range) = {
^
Main.scala:47: error: expected class or object definition
printGoldbachList: (Range)Unit
^
10 errors found
stdout
Standard output is empty