Project Euler problem 92 in Scala, square digit chains Here is a summary of the problem: 
"A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before... How many starting numbers below ten million will arrive at 89?" The site also gives an example at https://projecteuler.net/problem=92.

I have a solution which arrives at the correct answer but takes ~24 seconds on my computer to complete. I used an array to record as true the index of each value which resolves to 89 through the chain described above. I used an array so that updating and checking values would take constant time. I believed that recording all numbers that resolved to 89 would save time so that the checkAll function could avoid redundant calls of chain for these values. 

However, I tested this assumption and wrote a version in which chain would simply recurse until resolving to true or false without recording the chain, so that checkAll would check every single value from 1 until 10 million but perhaps save time from not recording values. Despite the change, this version took almost the exact same amount of time, although at least it's more concise.

For the above reason I now suspect that the chain of method calls used to produce the next variable in the chain function is the main bottleneck, but I am not sure whether this is possible to implement in a faster way. 

object euler92 {
  def main(args: Array[String]): Unit = {
    val t0 = System.nanoTime()

    val blank = new Array[Boolean](10000000)
    def chain(number: Int, links: List[Int] = List()): Unit = {
      val next = number.toString.toList.map(_.toString).map(_.toInt).map(x =&gt; x * x).sum
      if (next == 89) for (i &lt;- number :: links) blank(i) = true
       else if (next == 1) ()
      else {
        chain(next, number :: links)
      }
    }
    def checkAll(num: Int, counter: Int = 0): Unit = {
      if (num == 10000000) println("Answer: " + counter)
      else if (blank(num) == true) checkAll(num + 1, counter + 1)
      else {
        chain(num)
        if (blank(num) == true) checkAll(num + 1, counter + 1)
        else checkAll(num + 1, counter)
      }
    }
    checkAll(1)

    val t1 = System.nanoTime()
    println("Elapsed time: " + (t1 - t0) + " ns")
  }
}


Here is the implementation without an array: 

def chain(number: Int, links: List[Int] = List()): Boolean = {
      val next = number.toString.toList.map(_.toString).map(_.toInt).map(x =&gt; x * x).sum
      if (next == 89) true
      else if (next == 1) false
      else {
        chain(next, number :: links)
      }
    }

def checkAll(num: Int, counter: Int = 0): Unit = {
  if (num == 10000000) println("Answer: " + counter)
  else {
    val ans = chain(num)
    if (ans) checkAll(num + 1, counter + 1)
    else checkAll(num + 1, counter)
  }
}
checkAll(1)


I am hoping for insight on how to improve the performance of this solution.
Let's start off with your code to convert to the sum of digits.

number.toString.toList.map(_.toString).map(_.toInt).map(x =&gt; x * x).sum


First of all, a String can already be treated as List[Char], so that toList is unnecessary. Also, instead of converting from Char to String to Int, you can just call asDigit. And while we're at it, let's stuff that in a function, so that we know what's going on when we call that line. After all that, the line looks like:

def sumSquareDigits(number: Int): Int = number.toString.map(_.asDigit).map(x =&gt; x*x).sum


Now, you had the right idea in creating a hash. However, as an array of Booleans it's hard to tell what's been set and what hasn't. For now, let's start with a HashMap, since it's the most obvious construct for a cache. If it were too slow you could worry more about optimization, but at least on my computer it's sufficient to solve it. This has the added advantage of not needing to cache values that aren't repeated. Instead of needing to fill in 10,000,000 integers (I don't have that kind of spare RAM), it only ends up needing to cache 495 values. Additionally, since we're using the cache inductively, we should prepopulate the cache with our base case:

val cache = scala.collection.mutable.HashMap[Int,Int](1-&gt;1,89-&gt;89)


Now, we want to see whether a value converges to 1 or 89. We can do this by recursively calling the hash on the sumSquareDigits defined above until we get an answer, placing the results of any new stuff into the cache. We'll use a helper function just for simplicity:

def addValueToCache(number: Int) =
{
  val converged = getConvergedValue(number)
  cache.put(number,converged)
  converged
}
def getConvergedValue(number: Int) = cache.getOrElse(sumSquareDigit(x), addValueToCache(sumSquareDigit(x))


And now we just need to check which values converge to 89, 

def checkAll(max: Int): Int= 
  (1 until max).map(getConvergedValue).filter(_ == 89).size


then call it from main!

def main(args: Array[String]): Unit =
{
  println("Answer: "+checkAll(10000000))
}


Notice how using if statements for flow control is entirely unnecessary here. Functional programming style tends to do flow control with monadic operations (filter, map, flatMap, flatten, etc...) or pattern matching. If you're trying to stick to a functional style, when you see a lot of nested if statements you should be wondering if there's a better way to do it.