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