Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. // fibonacci is a function that returns
  6. // a function that returns an int.
  7. func fibonacci() func() int {
  8. a := 1
  9. b := 1
  10. c := 0
  11. n := 0
  12. return func() int {
  13. n += 1
  14. if n < 3 {return 1}
  15.  
  16. c = a + b
  17. a, b = b, c
  18.  
  19. return b
  20. }
  21. }
  22.  
  23. func main() {
  24. f := fibonacci()
  25. for i := 0; i < 10; i++ {
  26. fmt.Println(f())
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement