Wednesday 30 January 2019

Groovy: print first 10 fibonacci numbers


HelloWorld.groovy
previous = 1
current = 1

print previous + " " + current

8.times{
 next =  previous + current
 previous  = current
 current  = next
 print " " + next
}

Output
1 1 2 3 5 8 13 21 34 55


Previous                                                 Next                                                 Home

2 comments:

  1. def fib(n){
    a=0
    b=1
    while (b < n ){
    t=a
    a=b
    b = a+t
    println a
    }
    }
    fib(100)

    ReplyDelete