Scala 获取从1到1000可被3或5整除的元素列表
我正在尝试用scala编写一个函数方法,以得到一个1到1000之间所有可被3或5整除的数字的列表 以下是我到目前为止的情况:Scala 获取从1到1000可被3或5整除的元素列表,scala,Scala,我正在尝试用scala编写一个函数方法,以得到一个1到1000之间所有可被3或5整除的数字的列表 以下是我到目前为止的情况: def getListOfElements(): List[Int] = { val list = List() for (i <- 0 until 1000) { //list. } list match { case Nil => 0 } list } def getListoFemen
def getListOfElements(): List[Int] = {
val list = List()
for (i <- 0 until 1000) {
//list.
}
list match {
case Nil => 0
}
list
}
def getListoFements():List[Int]={
val list=list()
对于(i)0
}
列表
}
for循环似乎是一种必须的方法,我不确定在case类中匹配什么。请提供一些指导?下面是如何使用
for
表达式
for( i <- 1 to 1000 if i % 3 == 0 || i % 5 == 0) yield i
下面是另一种对数字范围
进行过滤的方法
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
。例如res0。toList
看起来像是Brian打败了我:)
我只是想说,为了获得更好的性能,流可能更可取:
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 to 1000).filter(i => i % 3 == 0 || i % 5 == 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; 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>val t0=System.nanoTime;(对于(i List?)projecteuler.net中的问题还需要在末尾列出这些数字的总和
“求1000以下所有3或5的倍数之和。”
对象prb1{
def main(参数:数组[字符串]){
val retval=对于{a这是吗?@om nom nom它的toStream是一个滥杀;)它只有1000个元素。好吧,我想没有什么害处。如果你使用更大的范围,它可以立即扩展:)筛选器不是for+if的另一种方法,它完全相同。并且不要使用x toList
。这是一个后缀运算符,容易出错,因此隐藏在2.10中的标志后面。您的问题是什么?
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
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)
}
}