Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Scala'的三角形数字序列;s斐波那契_Scala - Fatal编程技术网

使用Scala'的三角形数字序列;s斐波那契

使用Scala'的三角形数字序列;s斐波那契,scala,Scala,目的是使用Scala的Fibionacci创建一个三角形数字序列(1,3,6,10,15,21),需要返回第六位数字 测试 test("triangular") { assert(Calculation.triangular(1, 3) === 21) } Main def triangular(a: Int, b: Int) : Int = { lazy val s: Stream[Int] = a #:: s.scanLeft(b)(_+_) s(5) } 结果 [info]

目的是使用Scala的Fibionacci创建一个
三角形数字序列
(1,3,6,10,15,21),需要返回第六位数字

测试

test("triangular") {
  assert(Calculation.triangular(1, 3) === 21)
}
Main

def triangular(a: Int, b: Int) : Int = {
  lazy val s: Stream[Int] = a #:: s.scanLeft(b)(_+_)
  s(5)
}
结果

[info] - triangular *** FAILED ***
[info]   18 did not equal 21 (CalculationTest.scala:37)
[error] Failed: Total 9, Failed 1, Errors 0, Passed 8
[error] Failed tests:
[error]         testingscala.CalculationTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 5 s, completed Jul 27, 2014 7:55:16 PM
scanleet
将用于创建斐波那契序列,但不会创建三角形序列


需要使用哪个选项将2、3、4、5和随后的6添加到最后一个数字,这将导致一系列的
1、3、6、10、15、21

类似的结果如何

val triangular: Stream[Int] = Stream.from(2).scanLeft(1)(_+_)

对不起,我可能应该解释一下这是怎么回事。Stream.from创建从2开始的无限整数流,scanLeft将1作为起始数字,然后将上一个结果添加到无限流中的下一项。