-
-
Save parasyte/4077694 to your computer and use it in GitHub Desktop.
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
x, y := 0, 1 | |
return func() int { | |
x, y = y, x + y | |
return x | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
@RomanValihura: Here is another way with defer.
func fibonacci() func() int {
a, b := 0, 1
return func() int {
defer func() {
a = a + b
a, b = b, a
}()
return a
}
}
this anonymous function is even better
defer func() {
a, b = b, a + b
}()
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
first:=0
second:=1
var myFn =func() int{
result:=first
temp:=first
first=second
second=temp+second
return result;
}
return myFn
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
package main
func fib(n int) int {
fn := make(map[int]int)
for i := 0; i <= n; i++ {
var f int
if i <= 2 {
f = 1
} else {
f = fn[i-1] + fn[i-2]
}
fn[i] = f
}
return fn[n]
}
func main() {
var x = fib(100)
println(x)
}
Linear O(n) implementation as seen in Youtube Video Lecture
3736710778780434371
./fibonacci 10 1000 10000 20000
0,00s user 0,00s system 48% cpu 0,009 total
package main
import (
"fmt"
"math/big"
"os"
"strconv"
)
func fib(n int) *big.Int {
fn := make(map[int]*big.Int)
for i := 0; i <= n; i++ {
var f = big.NewInt(0)
if i <= 2 {
f.SetUint64(1)
} else {
f = f.Add(fn[i-1], fn[i-2])
}
fn[i] = f
}
return fn[n]
}
func main() {
for _, s := range os.Args[1:] {
n, e := strconv.Atoi(s)
if e != nil {
panic(e)
}
fmt.Printf("%s\n", fib(n))
}
}
My first implementation was like this because I had a background very different than Go. 😑
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
fib1 := 0
fib2 := 1
return func() int {
fib := fib1 + fib2
fib1 = fib2
fib2 = fib
return fib1
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
My Implementation:
The first number must be '0' in Fibonacci.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
switch {
case i == 0:
fmt.Println(i)
default:
fmt.Println(f())
}
}
}
Hi, guys. I just started to learn Golang, and I can't understand this summing
x, y = y, x + y
Can u help me with it?
@SorokinaT It's multiple assignments. Take a look at this stackoverflow thread.
The Fibonacci sequence is defined as: fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2)
.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y := 0, 1
return func() int {
result := x
x, y = y, x+y
return result
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Here is example with array using :)
package main
package main
import "fmt"
func fibonacci() func() int {
temp := [2] int { 0, 0 }
return func() int {
if temp[1] == 0 {
temp[1] = 1
return 0
}
temp[0], temp[1] = temp[1], temp[0]+temp[1]
return temp[0]
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Pretty Simple
package main
import "fmt"
func fibo(n int) int {
x, y := 0, 1
for i := 0; i < n; i++ {
x, y = y, x+y
}
return x
}
func main() {
var input int
fmt.Print("Enter the Number:")
fmt.Scanf("%d", &input)
fmt.Println("Result:", fibo(input))
}
Here is my shot starting from 0 :
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
tmp1, tmp2 := 0, 0
return func() int {
tmp1, tmp2 = tmp2, tmp1 + tmp2
if tmp2 == 0 { tmp1 = 1 }
return tmp2
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
goroutinue is useful !
/*
Windows
go build -o f1.exe f1.go && f1
or
go build -o f1.exe f1.go
f1 18
Linux
go build -o f1 f1.go && ./f1
or
go build -o f1 f1.go
./f1 18
*/
package main
import (
"fmt"
"os"
"strconv"
)
var length int
func Fibonacci(count int) {
//只写通道,用于传递数列上的数据项
ch := make(chan int)
//只读通道, 用于标记程序是否结束
shouldQuit := make(chan bool)
//不停的读取 ch 的内容,直到读完给定长度 count 个数据.
go func() {
for i := 0; i < count; i++ {
n := <-ch
fmt.Printf("%d, ", n)
}
shouldQuit <- true
}()
//第1项,第2项都为1
x, y := 0, 1
//开始无限循环
for {
//监听数据流动
select {
//数据通道可写入时,把x传递进去,并执行数列规则
case ch <- x:
//数列规则是:后一项是前两项的和
x, y = y, x+y
case isDone := <-shouldQuit:
fmt.Println("\nisDoen?", isDone)
return
}
}
}
func GetLength() {
n := 12
if len(os.Args) == 2 {
n, _ = strconv.Atoi(os.Args[1])
}
length = n
}
func main() {
GetLength()
// 获取该数列上的多少个数字
Fibonacci(length)
}
No need for conditions
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int64 {
var nm2 int64 = -1
var res int64 = 1
return func() int64 {
res, nm2 = res+nm2, res
return res
}
}
func main() {
f := fibonacci()
for i := 0; i < 100; i++ {
fmt.Println(f())
}
}
@RomanValihura: Here is another way with defer.
func fibonacci() func() int { a, b := 0, 1 return func() int { defer func() { a = a + b a, b = b, a }() return a } }
This solution is elegant.
this anonymous function is even better
defer func() { a, b = b, a + b }()
This looks good. 👍
Following code generates reverse fibonacci series (Enhanced code from this blog) - Purna Ramini
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y := 0, 1
return func() (r int) {
r = x
x, y = y, x + y
return
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
defer fmt.Println(f())
}
}
This gist is 7 years going strong!
FWIW, the best solution for computing Nth-Fib is the closed-form expression. See: https://gist.github.com/parasyte/cef9b3552d837f7a1e14b0be29918437
package main
import (
"fmt"
"log"
"time"
)
func fibByChannel(val int64) <-chan int64 {
result := make(chan int64)
go func() {
defer close(result)
if val < 2 {
result <- val
return
}
result <- <-fibByChannel(val - 1) + <-fibByChannel(val - 2)
}()
return result
}
func fibByNormal(num int) int {
if num < 2 {
return num
}
return fibByNormal(num-2)+fibByNormal(num-1)
}
func main() {
defer fmt.Printf("\nDONE")
timeStart := time.Now()
log.Printf("***** %v *****","Begin of fibByChannel")
log.Printf("%v\n", <-fibByChannel(10))
log.Printf("***** %v ***** - [%v]","End of fibByChannel",time.Since(timeStart).Seconds())
timeStart = time.Now()
log.Printf("***** %v *****","Begin of fibByNormal")
log.Printf("%v\n", fibByNormal(10))
log.Printf("***** %v ***** - [%v]","End of fibByNormal",time.Since(timeStart).Seconds())
}
/* *********************************************************************************************** */
//2019/08/25 01:18:38 ***** Begin of fibByChannel *****
//2019/08/25 01:18:38 55
//2019/08/25 01:18:38 ***** End of fibByChannel ***** - [0.00045481]
//2019/08/25 01:18:38 ***** Begin of fibByNormal *****
//2019/08/25 01:18:38 55
//2019/08/25 01:18:38 ***** End of fibByNormal ***** - [4.158e-06]
//DONE⏎
Very late but here's my solution. This also makes sure that the pattern starts from 0.
func fibonacci() func() int {
a, b, c := 0, 1, 0
return func() int {
c += a
a = b
b = c
return c
}
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
result := 0
initial := 1
return func() int {
temp := result
result += initial
initial = temp
return initial
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
i := 0
iOne := int(1)
ret := 0
return func() int {
if i == 0{
i = 1
}
iOne = ret
ret = i + iOne
i = iOne
return ret
}
}
func main() {
f := fibonacci()
for i := 0; true; i++ {
fmt.Println(f())
}
}
func fibonacci() func() int {
x, y := -1, 0
return func() int {
x, y = y, x + y
return (-1) * x
}
}
package main
import "fmt"
func main() {
a := 0
for i:=1; i<=1000; {
fmt.Println(i)
a, i = i, i+a
}
}
func fibonacci() func() int {
a := 0
b := 1
return func() int {
defer func() {
fib := a+b
a = b
b = fib
}()
return a
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
func Fibonacci() func() uint {
var a, b uint = 1, 0
return func() uint {
a, b = b, a+b
return a
}
}
thanks for the gist,it is very much useful
I make Benchmarking for deferent Fibonacci functions and algorithms with running unit test
https://github.com/msh2050/GoFibonacciBench
My implementation