I'm trying to solve PRIME1 in SPOJ with the following Scala snippet:Peter wants to generate some prime numbers for his cryptosystem. Help  him! Your task is to generate all prime numbers between two given  numbers! InputThe input begins with the number \$t\$ of test cases in a single line  (\$t \le 0\$). In each of the next t lines there are two numbers \$m\$ and \$n\$ (\$1 \le m \le n \le 1000000000, n-m \le 100000\$) separated by a space. OutputFor every test case print all prime numbers \$p\$ such that \$m <= p <= n\$,  one number per line, test cases separated by an empty line.object _002_PRIME1 extends App {  import scala.io.StdIn._  /// ***1***  // def isPrime(num: Int) = (2 until num).view.takeWhile(i => i * i <= num).forall(i => i * (num / i) < num)  def sieve(m: Int, n: Int, step: Int, primeArray: Array[Boolean]): Unit = {    val start = {      if (m <= step) {        2 * step      } else {        val pre = (m / step) * step        if (pre == m) {          pre        } else {          pre + step        }      }    }    Range(start, n + 1, step).foreach(i => primeArray(i - m) = false)  }  def simpleSieve(num: Int): IndexedSeq[Int] = {    val basicPrimes = Array.fill(num - 1)(true)    for {      i <- 2 to num      if basicPrimes(i - 2)      j <- i * 2 to num by i    } basicPrimes(j - 2) = false    for {      j <- basicPrimes.indices      if basicPrimes(j)    } yield (j + 2)  }  def sieveAll(m: Int, n: Int): Unit = {    val primeArray = Array.fill(n - m + 1)(true)    val basicPrimes = simpleSieve(math.sqrt(n).toInt)    /// for(p<- basicPrimes)sieve(m, n, p, primeArray)    basicPrimes.foreach(p => sieve(m, n, p, primeArray))    if (m == 1) {      primeArray(0) = false    }    for (j <- primeArray.indices; if primeArray(j)) println(j + m)    /// ***2***    //    Array.tabulate(primeArray.length) { i => {    //      if (primeArray(i)) {    //        println(i + m)    //      }    //    }    //    }  }  (1 to readInt()).foreach(_ => {    val ss = readLine().split(\\s+).map(_.toInt)    val (m, n) = (ss.head, ss.last)    sieveAll(m, n)  })}For PRIME1 itself, this is able to output the correct answer within 0.6s. However for another problem, PRINT, which is almost the same except that the input range is bigger: \$2 \le L < U \le 2147483647, U-L \le 1000000\$, and the time limit is more restricted (1.223s).When I submit this code, it always tells that time limit exceeded. How can I tune this snippet to make it run faster?Attempts I've tried (the time costs are based on the OJ from PRIME1):Using isPrime for basic primes checking, this would increase the running time to 0.7s.Using Array.tabulate instead of for comprehension for output, decreasing time a little, about 0.01s.Additionally, from Daniel C. Sobral's answer in the other question, mutable.BitSet may be more memory efficient, however this answer mentions that as to Java, an array might be more CPU efficient. I therefore didn't use BitSet for Scala (the memory used by is about 314MB while the memory limited by SPOJ is 1536MB).
Segmented Sieve of Eratosthenes in Scala
programming challenge;primes;scala;time limit exceeded;sieve of eratosthenes