0

strong text//I am writing a scala function using for-comprehension, primes : Int -> List[Int] that takes a number n and returns a list of prime numbers between 2 and n. Using of for-comprehension is mandatory.

 def primes(n:Int) = 
   for (i <- 2 to n if n > 1) yield
   if (check(i)) println(i) 

  def check(a: Int): Boolean = {
 for (i <- 2 to a) {
    if (a % i != 0) true

 }}
//Example: primes(10) should give List(2,3,5,7)
Daniel
  • 1
  • 2

0 Answers0