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

后端 未结 6 625
北海茫月
北海茫月 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 13:13

    No any answer without division or list recreation. No any answer with recursion.

    Also, any benchmarking?

    @scala.annotation.tailrec def div3or5(list: Range, result: List[Int]): List[Int] = {
      var acc = result
      var tailList = list
      try {
        acc = list.drop(2).head :: acc   // drop 1 2 save 3
        acc = list.drop(4).head :: acc   // drop 3 4 save 5
        acc = list.drop(5).head :: acc   // drop 5 save 6 
        acc = list.drop(8).head :: acc   // drop 6 7 8 save 9
        acc = list.drop(9).head :: acc   // drop 9 save 10
        acc = list.drop(11).head :: acc  // drop 10 11 save 12
        acc = list.drop(14).head :: acc  // drop 12 13 14 save 15 
        tailList = list.drop(15)         // drop 15             
      } catch {
        case e: NoSuchElementException => return acc // found
      }
      div3or5(tailList, acc) // continue search  
    }
    
    div3or5(Range(1, 1001), Nil)
    

    EDIT

    scala> val t0 = System.nanoTime; div3or5(Range(1, 10000001), Nil).toList; 
    (System.nanoTime - t0) / 1000000000.0
    t0: Long = 1355346955285989000
    res20: Double = 6.218004
    

    One of answers that looks good to me:

    scala> val t0 = System.nanoTime; Range(1, 10000001).filter(i => 
    i % 3 == 0 || i % 5 == 0).toList; (System.nanoTime - t0) / 1000000000.0
    java.lang.OutOfMemoryError: Java heap space
    

    Another one:

    scala> val t0 = System.nanoTime; (Range(1, 10000001).toStream filter (
    (t: Int)=>(t%3==0)||(t%5==0))).toList ; (System.nanoTime - t0) / 1000000000.0
    java.lang.OutOfMemoryError: Java heap space
    

    First one:

    scala> val t0 = System.nanoTime; (for( i <- 1 to 10000000 if i % 3 == 0 || 
    i % 5 == 0) yield i).toList; (System.nanoTime - t0) / 1000000000.0
    java.lang.OutOfMemoryError: Java heap space
    

    Why Scala does not optimize for example Vector -> List?

提交回复
热议问题