I solved this prime number verifier problem:
import scala.io.StdIn
object Main extends App {
val testCases = StdIn.readInt
for (i <- 0 until testCases) {
val limits = StdIn.readLine.split(" ")
val m: Int = limits(0).toInt
val n: Int = limits(1).toInt
for (j <- m to n) {
var prime = false
if (isPrime(j))
println(j)
}
println()
}
def isPrime(number: Int) : Boolean = {
if (number == 1)
false
else {
for (h <- 2 to Math.sqrt(number).asInstanceOf[Int]) {
if (number % h == 0 || number == 1) {
return false
}
}
true
}
}
}
I wrote the code this way because Scala doesn't have break
statements, and return
statements aren't very functional either. How do I go about writing this code in a more functional and idiomatic manner?