我对Groovy很新。我正在用它的属性来解决斐波那契数列。我有一个像这样的groovy代码:
a = new BigInteger[2][2]
a[0][0] = 1
a[0][1] = 1
a[1][0] = 1
a[1][1] = 0
temp = new BigInteger[2][2]
temp = a
def testFun(def a,BigInteger n) {
if(n == 1)
return a
b = new BigInteger[2][2]
def sum = 0
for ( c = 0 ; c < 2 ; c++) {
for ( d = 0 ; d < 2 ; d++ )
{
for ( k = 0 ; k < 2 ; k++ )
{
sum = sum + temp[c][k] * a[k][d];
}
b[c][d] = sum
sum = 0;
}
}
testFun(b,n-1)
}
c = new BigInteger[2][2]
BigInteger n = 90
if( n % 2 == 0 )
{
c = testFun(a,n.divide(2))
temp = c
c = testFun(c,2)
}
println c[0][1]
我得到的答案如下:
2880067194370816120
但是,当我将n值更改为5090
时,我会收到如下错误:
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
at fibb.testFun(fibb.groovy:24)
at fibb$testFun.callCurrent(Unknown Source)
出了什么问题?为什么我收到此错误?
答案 0 :(得分:2)
虽然我原本希望在该堆栈跟踪中看到StackOverflowException,但也许你只是将其剪掉了。我认为你正在遇到最大的递归深度,这就是为什么它适用于n
的小值,但是失败的值很大。你想看看关闭trampolining,Groovy 1.8的一部分。以下是release notes的相关部分。
还有this question类似,应该可以帮到你。
顺便说一下,它在堆栈跟踪中显示“Unknown Source”的原因是因为Groovy没有可用的源来告诉你它在哪一行 - 它是一个红色的鲱鱼。