I wrote small app finding next prime number starting from number passed as argument:
object HelloWorld extends App {
println(findNextPrime(49));
def findNextPrime(n: Int) : Int = {
var m = n;
while(true){
m += 1;
if(isPrime(m)){
return m;
}
}
throw new Exception("Something went wrong.");
}
def isPrime(n: Int): Boolean = {
n match {
case 0|1 => return false
case 2|3 => return true
case _ => {
(2 to Math.sqrt(n).toInt).forall(y => n%y!=0)
}
}
}
}
Is there better (more functional way) to wrote findNextPrime
in Scala ?