我正在尝试解决这个问题http://projecteuler.net/problem=62并且我被这个错误挂断了:
euler.scala:10: error: type mismatch;
found : Array[Any]
required: Array[Int]
Note: Any >: Int, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: Int`. (SLS 3.2.10)
master(perm) = if (master.contains(perm)) master(perm) :+ cube else Array(cube)
^
one error found
问题可能是因为 BigInt 试图存储在一个数组中,但显然没有 Array[BigInt] 的数组这样的东西
下面是我的代码:
import scala.util.control.Breaks._
var m = new scala.collection.mutable.LinkedHashMap[String,Array[Int]]
var master = m.withDefaultValue(Array.empty[Int])
val range = 345 to 9999
range.foreach { n =>
val cube = BigInt(n) * n * n
val perm = cube.toString.map(_.asDigit).mkString("")
master(perm) = if (master.contains(perm)) master(perm) :+ cube else Array(cube)
}
master.values.foreach { value =>
if (value.length >= 5) {
println (Math.cbrt(value(0)))
break
}
}