Last active
September 10, 2019 14:35
-
-
Save mrjohannchang/17bb1f260bf139ef0567b5a2be4ae2ed to your computer and use it in GitHub Desktop.
[Interview] Please modify the program, so the Fibonacci sequence starts from 0 instead of 1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2012 The Go Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
// +build ignore | |
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
f, g := 0, 1 | |
return func() int { | |
f, g = g, f+g | |
return f | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment