Get list of elements that are divisible by 3 or 5 from 1 - 1000

后端 未结 6 606
北海茫月
北海茫月 2021-01-14 12:30

I\'m trying to write a functional approach in scala to get a list of all numbers between 1 & 1000 that are divisible by 3 or 5

Here is what I have so far :

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-14 12:50

    Looks like Brian beat me to it :)

    Just thought I'd mention that a Stream might be more preferable here for better performance:

    val x = (1 until 1000).toStream           //> x  : scala.collection.immutable.Stream[Int] = Stream(1, ?)
    x filter (t=>(t%3==0)||(t%5==0))          //> res0: scala.collection.immutable.Stream[Int] = Stream(3, ?)
    

提交回复
热议问题