斐波那契数列,递归实现
func fibonacci(num int) (res int){
if num <= 1{
return 1
} else {
return fibonacci(num - 1) + fibonacci(num - 2)
}
}
闭包实现斐波那契数列,非递归
func fi() func(int) int{
a,b := 1,1
return func(i int) int{
if i <= 2{
return 1
}
for x:=3;x<=i;x++{
a,b = b,a+b
}
return a+b
}
}
网友评论