Groovy web console

subscribe to the feed Subscribe
to this
site
Recursive Fibonacci with memory-optimal memoize (via #groovywebconsole)
tweet this snippet Tweet
this
script

Recursive Fibonacci with memory-optimal memoize

Published 3 years ago by Vaclav Pech with tags memoize
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def fib = null
fib = {
    it <= 1 ? it : (fib(it - 2) + fib(it - 1))
}.memoizeAtMost(2)

long b = System.currentTimeMillis()
fib(35G)
long a = System.currentTimeMillis()
println((a-b) / 1000 + ' seconds') // about 0.08 seconds on my machine