Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

November 05 2008

robi42
21:38

Groovy Fibber

So, here's a (rather) simple Fibonacci.groovy:
/**
* Calculating and printing out a
* certain amount of Fibonacci numbers.
*/
class Fibber {
BigInteger current, next, newCurrent

// does the actual job:
def calcAndPrint(numberOf) {
current = 1; next = 1
numberOf.times {
print "$current "
newCurrent = next
next += current
current = newCurrent
}; println ''
}
}

try {
def amount = args[0].toInteger()
new Fibber().calcAndPrint(amount)
} catch (e) {
println 'usage: groovy Fibonacci.groovy <amountNumber>'
}

Now, e.g., this shell input:
$ groovy Fibonacci.groovy 13
results in this output:
1 1 2 3 5 8 13 21 34 55 89 144 233 

:-)

Update: Here's how you could do this with Helma NG.