2012-12-12 15 views
3

我試圖寫在斯卡拉功能的方法來獲得1 & 1000由3或5獲取是被3整除或5 1的元素列表 - 1000

整除之間的所有號碼的列表

這是我到目前爲止有:

def getListOfElements(): List[Int] = { 
    val list = List() 

    for (i <- 0 until 1000) { 
     //list. 
    } 
    list match { 
    case Nil => 0 
    } 
    list 
} 

for循環似乎是勢在必行的做法,我不知道在案例類匹配的內容。請指導一些?

+4

這是[fizzbuzz](http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html )? –

+0

@ om-nom-nom它的http://projecteuler.net/problem=1 –

回答

6

下面是我將如何使用for表達式來完成此操作。

for(i <- 1 to 1000 if i % 3 == 0 || i % 5 == 0) yield i 

這給:

scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 6, 9, 10, 12, 15, 18, 20, 21... 

這裏的另一種方法對數字的Range過濾。

scala> 1 to 1000 
res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10... 


scala> res0.filter(x => x % 3 == 0 || x % 5 == 0) 
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 6, 9, 10, 12, 15, 18, 20, 21... 

如果你真的想在返回值使用toList一個List。例如res0.toList

+1

過濾器不是另一種for +的方法,如果它完全一樣,也不要使用'x toList'。一個後綴運算符,容易出錯,因此隱藏在2.10中的一個標誌後面 – sschaef

2

看起來像布萊恩打我吧:)

只是想我會提到,一個流可能是更優選這裏有更好的表現:

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, ?) 
+1

toStream是一種矯枉過正的行爲;)它只有1000個元素 – Jakozaur

+1

好吧,我猜沒有什麼壞處,如果你使用更大的範圍:) –

4
(Range(3, 1000, 3) ++ Range(5, 1000, 5)).toSet.toList.sorted 

排序可以省略。

3

另一形式給出:

(1 to 1000).filter(i => i % 3 == 0 || i % 5 == 0) 
0

沒有任何部門或列表娛樂任何回答。遞歸沒有任何答案。

此外,任何基準?

@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) 

編輯

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

一個答案,那對我來說很好的:

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 

還有一句:

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 

第一招:

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 

爲什麼Scala不優化例如Vector - > List?

2

projecteuler.net的問題也希望這些數字的總和。

「找到1000以下所有3或5的倍數之和。「

object prb1 { 
    def main(args: Array[String]) { 
    val retval = for{ a <- 1 to 999 
         if a % 3 == 0 || a % 5 == 0 
    } yield a 
    val sum = retval.reduceLeft[Int](_+_) 
    println("The sum of all multiples of 3 and 5 below 1000 is " + sum) 
    } 
} 

正確的答案應該是

+0

你的問題到底是什麼? –

相關問題