1

我对 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)

什么地方出了错?为什么我会收到此错误?

4

1 回答 1

2

尽管我本来希望在该堆栈跟踪中看到 StackOverflowException,但也许您只是将其剪掉了。我认为您遇到了最大递归深度,这就是为什么它适用于较小的值n但会因较大的值而失败。您想研究 Groovy 1.8 的一部分 Closure trampolining。这是发行说明的相关部分。

还有一个类似的问题,应该可以帮助您。

By the way, the reason it says "Unknown Source" in the stack trace is because Groovy doesn't have its source available to tell you which line it was on - it's a red herring.

于 2012-07-18T17:48:01.967 回答